module Accelerator.SM90.Lowering import Accelerator.SM86.Control import Accelerator.SM86.Immediate import Accelerator.SM86.Instruction import Accelerator.SM86.InstructionEncoding import Accelerator.SM86.Types import Accelerator.SM90.Instruction import Accelerator.SM90.InstructionEncoding import Accelerator.SM90.Word import Hardware.Nvidia.SM86.Command.WholeProgramPlan import Std.Natural -- The sm_90 realization of an SM86-shaped program: what an H100 runs for it. -- -- It is word for word. Hopper decodes almost every word Accelerator.SM86's -- encoder emits exactly as Ampere does (the header, the register files, the -- ALU, conversion, tensor-core, shared-memory, shuffle, barrier and -- asynchronous-group forms), so each such instruction keeps its SM86 word. -- Evidence: every distinct word of the GPT-2 trainer and initialization -- programs, nvdisasm -b SM86 against -b SM90 (CUDA 12.4, 2026-10-10, -- comparisons/h100/README.md), and one instance of each SM86 constructor -- (CUDA 12.9, reference/oracles/nvidia-sm90-ptxas/sm86-words/). The -- forms whose bits must change are re-encoded from the typed instruction by -- Accelerator.SM90.InstructionEncoding: -- -- * LDG / LDG.128 / STG / STG.64 / STG.128 / LDGSTS take the raw global -- form, [Ra.64+URZ+offset], with no memory descriptor; -- * BRA's displacement moves to Hopper's split field; -- * RED.ADD.F32 takes the raw form too, REDG [Ra.64+URZ+offset]: its -- descriptor flag is bit 71 (bit 76 is its operand type), measured -- against ptxas and nvdisasm (reference/oracles/nvidia-sm90-ptxas/ -- global-branch/red.sass) and run on the H100 (2026-10-19, the -- speedrun backward's value-embedding probe). -- -- Because nothing is inserted or removed, the program keeps its length: -- every launch placement, every region extent and every branch displacement -- derived from its SM86 encoding holds for its sm_90 code unchanged. And -- because each word is a function of its own instruction, the backend can -- apply this lowering after it has allocated a region's registers -- (NvidiaDeviceAllocatedProgramRegion), which a whole-program lowering -- computed from the program as written could not. def sm90Architecture : Nat = 0x90 def sm90InstructionRefused = (lambda unrestricted reason : Bytes . constructor NvidiaInstructionRealization NvidiaInstructionRealizationRefused reason) def sm90RefusalText = (lambda unrestricted reason : SM90EncodingRefusal . eliminate SM90EncodingRefusal (lambda unrestricted current : SM90EncodingRefusal . Bytes) reason (branch SM90RefusalGlobalOffsetOutOfRange . b"an sm_90 global access offset is at or above 2^23") (branch SM90RefusalCopyOffsetOutOfRange . b"an sm_90 LDGSTS offset exceeds its field") (branch SM90RefusalControlOutOfRange . b"an sm_90 instruction's scheduling control exceeds its field") (branch SM90RefusalBranchUnaligned . b"an sm_90 branch displacement is not a multiple of 4") (branch SM90RefusalBranchOutOfRange . b"an sm_90 branch displacement exceeds its field") (branch SM90RefusalUnknownOpcode . b"an sm_90 word has an unknown opcode") (branch SM90RefusalAddressUnaligned . b"an sm_90 global address register is not an even pair") (branch SM90RefusalDataUnaligned . b"an sm_90 wide access's data register is not aligned to its width") (branch SM90RefusalSelectorOutOfRange . b"an sm_90 byte permutation selector exceeds 16 bits") (branch SM90RefusalNotCanonical . b"an sm_90 word is not its form's canonical encoding")) -- An sm_90 word from Accelerator.SM90's encoder. def sm90RealizeEncoding = (lambda unrestricted encoding : SM90Encoding . eliminate SM90Encoding (lambda unrestricted current : SM90Encoding . NvidiaInstructionRealization) encoding (branch SM90Encoded word . constructor NvidiaInstructionRealization NvidiaInstructionRealized (sm90WordBytes word)) (branch SM90EncodingRefused reason . sm90InstructionRefused (sm90RefusalText reason))) -- The instruction's SM86 word, which Hopper decodes as Ampere does: the -- backend places the canonical word its SM86 encoder produced for the -- region (WholeProgramPlan's NvidiaInstructionKeepsCanonicalWord). def sm90KeepSM86Word = (lambda unrestricted instruction : SM86Instruction . NvidiaInstructionKeepsCanonicalWord) -- SM86's BRA holds its displacement as a 32-bit two's complement and its -- sign extension in the descriptor's low 18 bits (0x03800000 forward, -- 0x0383ffff backward; Accelerator.SM86.InstructionEncoding's -- sm86EncodeBranch). Any other descriptor is not a displacement this -- lowering can read, and is refused. def sm90ForwardDescriptor : Nat = 0x03800000 def sm90BackwardDescriptor : Nat = 0x0383ffff def sm90SignBit32 : Nat = 0x80000000 def sm90Modulus32 : Nat = 0x100000000 def sm90LowerBranch = (lambda unrestricted guard : SM86InstructionGuard . unrestricted offset : SM86Unsigned32 . unrestricted descriptor : SM86Unsigned32 . unrestricted control : SM86Control . let unrestricted displacement = (sm86Unsigned32Natural offset) in let unrestricted sign = (sm86Unsigned32Natural descriptor) in sm90Choose NvidiaInstructionRealization (naturalAnd (naturalEqual sign sm90ForwardDescriptor) (nat-less-than displacement sm90SignBit32)) (sm90RealizeEncoding (sm90EncodeBranch guard (constructor SM90BranchTarget SM90BranchForward displacement) control)) (sm90Choose NvidiaInstructionRealization (naturalAnd (naturalEqual sign sm90BackwardDescriptor) (naturalLessOrEqual sm90SignBit32 displacement)) (sm90RealizeEncoding (sm90EncodeBranch guard (constructor SM90BranchTarget SM90BranchBackward (nat-subtract sm90Modulus32 displacement)) control)) (sm90InstructionRefused b"an SM86 BRA's descriptor is not its displacement's sign extension"))) def sm90Global32 = SM90GlobalLoad32 -- One SM86 instruction's sm_90 word. -- Register-only system vocabulary shared with AlphaOS. NVIDIA nvdisasm -- decodes these same canonical SM86 words on SM90; the recorded comparison -- is reference/oracles/nvidia-sm90-ptxas/register-system/. This is decoding -- evidence, not a silicon execution claim. Memory and indirect/system -- control forms remain refused until given their own measured lowering. def sm90RegisterSystemAllowed = (lambda unrestricted operation : SM86SystemOperation . eliminate SM86SystemOperation (lambda unrestricted current : SM86SystemOperation . Nat) operation (branch SM86AtomicGlobal f0 f1 f2 f3 f4 f5 . 0) (branch SM86AtomicCompareSwapGlobal f0 f1 f2 f3 f4 f5 . 0) (branch SM86ReduceGlobal f0 f1 f2 f3 f4 . 0) (branch SM86MemoryBarrier f0 f1 . 0) (branch SM86ErrorBarrier . 0) (branch SM86InvalidateDataCache . 0) (branch SM86LoadGlobalStrong f0 f1 f2 f3 . 0) (branch SM86StoreGlobalStrong f0 f1 f2 f3 . 0) (branch SM86Sleep f0 . 0) (branch SM86CompareRegisters f0 f1 f2 f3 f4 . 1) (branch SM86SelectRegisters f0 f1 f2 f3 f4 . 1) (branch SM86IntegerMinimumOrMaximum f0 f1 f2 f3 f4 . 1) (branch SM86MoveRegister f0 f1 . 1) (branch SM86IntegerMultiplyAddRegister f0 f1 f2 f3 . 1) (branch SM86ReadSystemRegister f0 f1 . 0) (branch SM86BranchIndirect f0 f1 . 0) (branch SM86CallIndirect f0 f1 . 0) (branch SM86ReturnIndirect f0 f1 . 0) (branch SM86PopulationCount f0 f1 . 0) (branch SM86VoteBallot f0 f1 f2 . 0) (branch SM86BarrierArrive f0 f1 . 0) (branch SM86ConvergenceStart f0 f1 . 0) (branch SM86ConvergenceWait f0 . 0) (branch SM86LabelDistance f0 f1 f2 . 0) ) def sm90LowerInstruction = (lambda unrestricted instruction : SM86Instruction . eliminate SM86Instruction (lambda unrestricted current : SM86Instruction . NvidiaInstructionRealization) instruction (branch SM86InstructionValue guard body . eliminate SM86InstructionBody (lambda unrestricted current : SM86InstructionBody . NvidiaInstructionRealization) body (branch SM86MoveConstant a b c d . sm90KeepSM86Word instruction) (branch SM86SpecialToRegister a b c . sm90KeepSM86Word instruction) (branch SM86MoveImmediate a b c . sm90KeepSM86Word instruction) (branch SM86IntegerMultiplyAddConstant a b c d e f . sm90KeepSM86Word instruction) (branch SM86IntegerMultiplyAddImmediate a b c d e . sm90KeepSM86Word instruction) (branch SM86IntegerMultiplyAddWideConstant a b c d e f . sm90KeepSM86Word instruction) (branch SM86IntegerAddThreeImmediate a b c d . sm90KeepSM86Word instruction) (branch SM86IntegerAddThreeRegister a b c d . sm90KeepSM86Word instruction) (branch SM86ShiftRightImmediate a b c d . sm90KeepSM86Word instruction) (branch SM86LogicThreeInputTruthTable a b c d e . sm90KeepSM86Word instruction) (branch SM86IntegerToFloat a b c . sm90KeepSM86Word instruction) (branch SM86FloatAdd a b c d . sm90KeepSM86Word instruction) (branch SM86FloatMultiply a b c d . sm90KeepSM86Word instruction) (branch SM86Float64Binary a b c d e . sm90KeepSM86Word instruction) (branch SM86FloatConvert a b . sm90KeepSM86Word instruction) (branch SM86FloatFusedMultiplyAdd a b c d e . sm90KeepSM86Word instruction) (branch SM86MultiFunctionUnitApproximation a b c d . sm90KeepSM86Word instruction) (branch SM86FloatMinimumOrMaximum a b c d e . sm90KeepSM86Word instruction) (branch SM86FloatNegate a b c . sm90KeepSM86Word instruction) (branch SM86FloatPairToPackedHalfPair a b c d . sm90KeepSM86Word instruction) (branch SM86FloatPairToPackedBFloat16Pair a b c d . sm90KeepSM86Word instruction) (branch SM86FloatPairToPackedFloat8Pair a b c d e f g . sm90KeepSM86Word instruction) (branch SM86FloatArithmetic a b c d e f g . sm90KeepSM86Word instruction) (branch SM86HalfToFloat a b c d . sm90KeepSM86Word instruction) (branch SM86BFloat16ToFloat a b c d . sm90KeepSM86Word instruction) (branch SM86TensorCoreHalfMatrixMultiplyAccumulate16x8x16Float32 a b c d e . sm90KeepSM86Word instruction) (branch SM86TensorCoreBFloat16MatrixMultiplyAccumulate16x8x16Float32 a b c d e . sm90KeepSM86Word instruction) (branch SM86TensorCoreHalfMatrixMultiplyAccumulate16x8x16Half a b c d e . sm90KeepSM86Word instruction) (branch SM86TensorCoreInteger8MatrixMultiplyAccumulate16x8x32Integer32 a b c d e . sm90KeepSM86Word instruction) (branch SM86LoadGlobal destination address offset control . sm90RealizeEncoding (sm90EncodeLoadGlobal guard sm90Global32 SM90LoadAllocate destination address (sm86Unsigned32Natural offset) control)) (branch SM86LoadGlobalWide destination address offset control . sm90RealizeEncoding (sm90EncodeLoadGlobal guard SM90GlobalLoad128 SM90LoadAllocate destination address (sm86Unsigned32Natural offset) control)) (branch SM86WarpShuffle a b c d e f . sm90KeepSM86Word instruction) (branch SM86LoadShared a b c d . sm90KeepSM86Word instruction) (branch SM86LoadSharedMatrix a b c d e f . sm90KeepSM86Word instruction) (branch SM86StoreShared a b c d . sm90KeepSM86Word instruction) (branch SM86LoadGlobalToShared address offset source sourceOffset control . sm90RealizeEncoding (sm90EncodeCopyGlobalToShared guard address (sm86Unsigned32Natural offset) source (sm86Unsigned32Natural sourceOffset) control)) (branch SM86CommitAsyncGroup a . sm90KeepSM86Word instruction) (branch SM86WaitAsyncGroups a b . sm90KeepSM86Word instruction) (branch SM86BarrierSynchronize a . sm90KeepSM86Word instruction) (branch SM86BarrierSynchronizeCount a b c . sm90KeepSM86Word instruction) (branch SM86StoreGlobal address value offset control . sm90RealizeEncoding (sm90EncodeStoreGlobal guard SM90GlobalStore32 address value (sm86Unsigned32Natural offset) control)) (branch SM86StoreGlobalWide address value offset control . sm90RealizeEncoding (sm90EncodeStoreGlobal guard SM90GlobalStore128 address value (sm86Unsigned32Natural offset) control)) (branch SM86StoreGlobal64 address value offset control . sm90RealizeEncoding (sm90EncodeStoreGlobal guard SM90GlobalStore64 address value (sm86Unsigned32Natural offset) control)) (branch SM86ReduceGlobalAddFloat32 address value offset control . sm90RealizeEncoding (sm90EncodeReduceGlobalAddFloat32 guard address value (sm86Unsigned32Natural offset) control)) (branch SM86PredicateGreaterThanImmediate a b c d . sm90KeepSM86Word instruction) (branch SM86Branch offset descriptor control . sm90LowerBranch guard offset descriptor control) (branch SM86Exit a . sm90KeepSM86Word instruction) (branch SM86System a b . sm90Choose NvidiaInstructionRealization (sm90RegisterSystemAllowed a) (sm90KeepSM86Word instruction) (sm90InstructionRefused b"an SM86 system form has no measured sm_90 encoding")))) -- The realization a device region carries for an sm_90 plan: the same for -- every program, since it is applied instruction by instruction. def sm90LowerRealization : NvidiaDeviceRealization = (constructor NvidiaDeviceRealization NvidiaDeviceRealizedEachInstruction sm90Architecture sm90LowerInstruction)