Source/Packages

Accelerator.SM90.Lowering

packages/hardware/architectures/nvidia-sm90/src/Accelerator/SM90/Lowering.alpha

209 lines14 declarations13.1 KiBSHA-256 a56c8a504d68

Complete file · line 81

Lowering.alpha

Definition view
1module Accelerator.SM90.Lowering
2
3import Accelerator.SM86.Control
4import Accelerator.SM86.Immediate
5import Accelerator.SM86.Instruction
6import Accelerator.SM86.InstructionEncoding
7import Accelerator.SM86.Types
8import Accelerator.SM90.Instruction
9import Accelerator.SM90.InstructionEncoding
10import Accelerator.SM90.Word
11import Hardware.Nvidia.SM86.Command.WholeProgramPlan
12import Std.Natural
13
14-- The sm_90 realization of an SM86-shaped program: what an H100 runs for it.
15--
16-- It is word for word.  Hopper decodes almost every word Accelerator.SM86's
17-- encoder emits exactly as Ampere does (the header, the register files, the
18-- ALU, conversion, tensor-core, shared-memory, shuffle, barrier and
19-- asynchronous-group forms), so each such instruction keeps its SM86 word.
20-- Evidence: every distinct word of the GPT-2 trainer and initialization
21-- programs, nvdisasm -b SM86 against -b SM90 (CUDA 12.4, 2026-10-10,
22-- comparisons/h100/README.md), and one instance of each SM86 constructor
23-- (CUDA 12.9, reference/oracles/nvidia-sm90-ptxas/sm86-words/).  The
24-- forms whose bits must change are re-encoded from the typed instruction by
25-- Accelerator.SM90.InstructionEncoding:
26--
27--   * LDG / LDG.128 / STG / STG.64 / STG.128 / LDGSTS take the raw global
28--     form, [Ra.64+URZ+offset], with no memory descriptor;
29--   * BRA's displacement moves to Hopper's split field;
30--   * RED.ADD.F32 takes the raw form too, REDG [Ra.64+URZ+offset]: its
31--     descriptor flag is bit 71 (bit 76 is its operand type), measured
32--     against ptxas and nvdisasm (reference/oracles/nvidia-sm90-ptxas/
33--     global-branch/red.sass) and run on the H100 (2026-10-19, the
34--     speedrun backward's value-embedding probe).
35--
36-- Because nothing is inserted or removed, the program keeps its length:
37-- every launch placement, every region extent and every branch displacement
38-- derived from its SM86 encoding holds for its sm_90 code unchanged.  And
39-- because each word is a function of its own instruction, the backend can
40-- apply this lowering after it has allocated a region's registers
41-- (NvidiaDeviceAllocatedProgramRegion), which a whole-program lowering
42-- computed from the program as written could not.
43
44def sm90Architecture : Nat = 0x90
45
46def sm90InstructionRefused = (lambda unrestricted reason : Bytes .
47  constructor NvidiaInstructionRealization NvidiaInstructionRealizationRefused reason)
48
49def sm90RefusalText = (lambda unrestricted reason : SM90EncodingRefusal .
50  eliminate SM90EncodingRefusal (lambda unrestricted current : SM90EncodingRefusal . Bytes) reason
51    (branch SM90RefusalGlobalOffsetOutOfRange . b"an sm_90 global access offset is at or above 2^23")
52    (branch SM90RefusalCopyOffsetOutOfRange . b"an sm_90 LDGSTS offset exceeds its field")
53    (branch SM90RefusalControlOutOfRange . b"an sm_90 instruction's scheduling control exceeds its field")
54    (branch SM90RefusalBranchUnaligned . b"an sm_90 branch displacement is not a multiple of 4")
55    (branch SM90RefusalBranchOutOfRange . b"an sm_90 branch displacement exceeds its field")
56    (branch SM90RefusalUnknownOpcode . b"an sm_90 word has an unknown opcode")
57    (branch SM90RefusalAddressUnaligned . b"an sm_90 global address register is not an even pair")
58    (branch SM90RefusalDataUnaligned . b"an sm_90 wide access's data register is not aligned to its width")
59    (branch SM90RefusalSelectorOutOfRange . b"an sm_90 byte permutation selector exceeds 16 bits")
60    (branch SM90RefusalNotCanonical . b"an sm_90 word is not its form's canonical encoding"))
61
62-- An sm_90 word from Accelerator.SM90's encoder.
63def sm90RealizeEncoding = (lambda unrestricted encoding : SM90Encoding .
64  eliminate SM90Encoding (lambda unrestricted current : SM90Encoding . NvidiaInstructionRealization) encoding
65    (branch SM90Encoded word .
66      constructor NvidiaInstructionRealization NvidiaInstructionRealized (sm90WordBytes word))
67    (branch SM90EncodingRefused reason . sm90InstructionRefused (sm90RefusalText reason)))
68
69-- The instruction's SM86 word, which Hopper decodes as Ampere does: the
70-- backend places the canonical word its SM86 encoder produced for the
71-- region (WholeProgramPlan's NvidiaInstructionKeepsCanonicalWord).
72def sm90KeepSM86Word = (lambda unrestricted instruction : SM86Instruction .
73  NvidiaInstructionKeepsCanonicalWord)
74
75-- SM86's BRA holds its displacement as a 32-bit two's complement and its
76-- sign extension in the descriptor's low 18 bits (0x03800000 forward,
77-- 0x0383ffff backward; Accelerator.SM86.InstructionEncoding's
78-- sm86EncodeBranch).  Any other descriptor is not a displacement this
79-- lowering can read, and is refused.
80def sm90ForwardDescriptor : Nat = 0x03800000
81def sm90BackwardDescriptor : Nat = 0x0383ffff
82def sm90SignBit32 : Nat = 0x80000000
83def sm90Modulus32 : Nat = 0x100000000
84
85def sm90LowerBranch =
86  (lambda unrestricted guard : SM86InstructionGuard .
87  unrestricted offset : SM86Unsigned32 .
88  unrestricted descriptor : SM86Unsigned32 .
89  unrestricted control : SM86Control .
90    let unrestricted displacement = (sm86Unsigned32Natural offset) in
91    let unrestricted sign = (sm86Unsigned32Natural descriptor) in
92    sm90Choose NvidiaInstructionRealization
93      (naturalAnd (naturalEqual sign sm90ForwardDescriptor) (nat-less-than displacement sm90SignBit32))
94      (sm90RealizeEncoding (sm90EncodeBranch guard (constructor SM90BranchTarget SM90BranchForward displacement) control))
95    (sm90Choose NvidiaInstructionRealization
96      (naturalAnd (naturalEqual sign sm90BackwardDescriptor) (naturalLessOrEqual sm90SignBit32 displacement))
97      (sm90RealizeEncoding (sm90EncodeBranch guard
98        (constructor SM90BranchTarget SM90BranchBackward (nat-subtract sm90Modulus32 displacement)) control))
99      (sm90InstructionRefused b"an SM86 BRA's descriptor is not its displacement's sign extension")))
100
101def sm90Global32 = SM90GlobalLoad32
102
103-- One SM86 instruction's sm_90 word.
104-- Register-only system vocabulary shared with AlphaOS. NVIDIA nvdisasm
105-- decodes these same canonical SM86 words on SM90; the recorded comparison
106-- is reference/oracles/nvidia-sm90-ptxas/register-system/. This is decoding
107-- evidence, not a silicon execution claim. Memory and indirect/system
108-- control forms remain refused until given their own measured lowering.
109def sm90RegisterSystemAllowed = (lambda unrestricted operation : SM86SystemOperation .
110  eliminate SM86SystemOperation (lambda unrestricted current : SM86SystemOperation . Nat) operation
111    (branch SM86AtomicGlobal f0 f1 f2 f3 f4 f5 . 0)
112    (branch SM86AtomicCompareSwapGlobal f0 f1 f2 f3 f4 f5 . 0)
113    (branch SM86ReduceGlobal f0 f1 f2 f3 f4 . 0)
114    (branch SM86MemoryBarrier f0 f1 . 0)
115    (branch SM86ErrorBarrier  . 0)
116    (branch SM86InvalidateDataCache  . 0)
117    (branch SM86LoadGlobalStrong f0 f1 f2 f3 . 0)
118    (branch SM86StoreGlobalStrong f0 f1 f2 f3 . 0)
119    (branch SM86Sleep f0 . 0)
120    (branch SM86CompareRegisters f0 f1 f2 f3 f4 . 1)
121    (branch SM86SelectRegisters f0 f1 f2 f3 f4 . 1)
122    (branch SM86IntegerMinimumOrMaximum f0 f1 f2 f3 f4 . 1)
123    (branch SM86MoveRegister f0 f1 . 1)
124    (branch SM86IntegerMultiplyAddRegister f0 f1 f2 f3 . 1)
125    (branch SM86ReadSystemRegister f0 f1 . 0)
126    (branch SM86BranchIndirect f0 f1 . 0)
127    (branch SM86CallIndirect f0 f1 . 0)
128    (branch SM86ReturnIndirect f0 f1 . 0)
129    (branch SM86PopulationCount f0 f1 . 0)
130    (branch SM86VoteBallot f0 f1 f2 . 0)
131    (branch SM86BarrierArrive f0 f1 . 0)
132    (branch SM86ConvergenceStart f0 f1 . 0)
133    (branch SM86ConvergenceWait f0 . 0)
134    (branch SM86LabelDistance f0 f1 f2 . 0)
135  )
136
137def sm90LowerInstruction = (lambda unrestricted instruction : SM86Instruction .
138  eliminate SM86Instruction (lambda unrestricted current : SM86Instruction . NvidiaInstructionRealization) instruction
139    (branch SM86InstructionValue guard body .
140      eliminate SM86InstructionBody (lambda unrestricted current : SM86InstructionBody . NvidiaInstructionRealization) body
141        (branch SM86MoveConstant a b c d . sm90KeepSM86Word instruction)
142        (branch SM86SpecialToRegister a b c . sm90KeepSM86Word instruction)
143        (branch SM86MoveImmediate a b c . sm90KeepSM86Word instruction)
144        (branch SM86IntegerMultiplyAddConstant a b c d e f . sm90KeepSM86Word instruction)
145        (branch SM86IntegerMultiplyAddImmediate a b c d e . sm90KeepSM86Word instruction)
146        (branch SM86IntegerMultiplyAddWideConstant a b c d e f . sm90KeepSM86Word instruction)
147        (branch SM86IntegerAddThreeImmediate a b c d . sm90KeepSM86Word instruction)
148        (branch SM86IntegerAddThreeRegister a b c d . sm90KeepSM86Word instruction)
149        (branch SM86ShiftRightImmediate a b c d . sm90KeepSM86Word instruction)
150        (branch SM86LogicThreeInputTruthTable a b c d e . sm90KeepSM86Word instruction)
151        (branch SM86IntegerToFloat a b c . sm90KeepSM86Word instruction)
152        (branch SM86FloatAdd a b c d . sm90KeepSM86Word instruction)
153        (branch SM86FloatMultiply a b c d . sm90KeepSM86Word instruction)
154        (branch SM86Float64Binary a b c d e . sm90KeepSM86Word instruction)
155        (branch SM86FloatConvert a b . sm90KeepSM86Word instruction)
156        (branch SM86FloatFusedMultiplyAdd a b c d e . sm90KeepSM86Word instruction)
157        (branch SM86MultiFunctionUnitApproximation a b c d . sm90KeepSM86Word instruction)
158        (branch SM86FloatMinimumOrMaximum a b c d e . sm90KeepSM86Word instruction)
159        (branch SM86FloatNegate a b c . sm90KeepSM86Word instruction)
160        (branch SM86FloatPairToPackedHalfPair a b c d . sm90KeepSM86Word instruction)
161        (branch SM86FloatPairToPackedBFloat16Pair a b c d . sm90KeepSM86Word instruction)
162        (branch SM86FloatPairToPackedFloat8Pair a b c d e f g . sm90KeepSM86Word instruction)
163        (branch SM86FloatArithmetic a b c d e f g . sm90KeepSM86Word instruction)
164        (branch SM86HalfToFloat a b c d . sm90KeepSM86Word instruction)
165        (branch SM86BFloat16ToFloat a b c d . sm90KeepSM86Word instruction)
166        (branch SM86TensorCoreHalfMatrixMultiplyAccumulate16x8x16Float32 a b c d e . sm90KeepSM86Word instruction)
167        (branch SM86TensorCoreBFloat16MatrixMultiplyAccumulate16x8x16Float32 a b c d e . sm90KeepSM86Word instruction)
168        (branch SM86TensorCoreHalfMatrixMultiplyAccumulate16x8x16Half a b c d e . sm90KeepSM86Word instruction)
169        (branch SM86TensorCoreInteger8MatrixMultiplyAccumulate16x8x32Integer32 a b c d e . sm90KeepSM86Word instruction)
170        (branch SM86LoadGlobal destination address offset control .
171          sm90RealizeEncoding (sm90EncodeLoadGlobal guard sm90Global32 SM90LoadAllocate
172            destination address (sm86Unsigned32Natural offset) control))
173        (branch SM86LoadGlobalWide destination address offset control .
174          sm90RealizeEncoding (sm90EncodeLoadGlobal guard SM90GlobalLoad128 SM90LoadAllocate
175            destination address (sm86Unsigned32Natural offset) control))
176        (branch SM86WarpShuffle a b c d e f . sm90KeepSM86Word instruction)
177        (branch SM86LoadShared a b c d . sm90KeepSM86Word instruction)
178        (branch SM86LoadSharedMatrix a b c d e f . sm90KeepSM86Word instruction)
179        (branch SM86StoreShared a b c d . sm90KeepSM86Word instruction)
180        (branch SM86LoadGlobalToShared address offset source sourceOffset control .
181          sm90RealizeEncoding (sm90EncodeCopyGlobalToShared guard
182            address (sm86Unsigned32Natural offset) source (sm86Unsigned32Natural sourceOffset) control))
183        (branch SM86CommitAsyncGroup a . sm90KeepSM86Word instruction)
184        (branch SM86WaitAsyncGroups a b . sm90KeepSM86Word instruction)
185        (branch SM86BarrierSynchronize a . sm90KeepSM86Word instruction)
186        (branch SM86BarrierSynchronizeCount a b c . sm90KeepSM86Word instruction)
187        (branch SM86StoreGlobal address value offset control .
188          sm90RealizeEncoding (sm90EncodeStoreGlobal guard SM90GlobalStore32
189            address value (sm86Unsigned32Natural offset) control))
190        (branch SM86StoreGlobalWide address value offset control .
191          sm90RealizeEncoding (sm90EncodeStoreGlobal guard SM90GlobalStore128
192            address value (sm86Unsigned32Natural offset) control))
193        (branch SM86StoreGlobal64 address value offset control .
194          sm90RealizeEncoding (sm90EncodeStoreGlobal guard SM90GlobalStore64
195            address value (sm86Unsigned32Natural offset) control))
196        (branch SM86ReduceGlobalAddFloat32 address value offset control .
197          sm90RealizeEncoding (sm90EncodeReduceGlobalAddFloat32 guard address value (sm86Unsigned32Natural offset) control))
198        (branch SM86PredicateGreaterThanImmediate a b c d . sm90KeepSM86Word instruction)
199        (branch SM86Branch offset descriptor control . sm90LowerBranch guard offset descriptor control)
200        (branch SM86Exit a . sm90KeepSM86Word instruction)
201      (branch SM86System a b .
202        sm90Choose NvidiaInstructionRealization (sm90RegisterSystemAllowed a)
203          (sm90KeepSM86Word instruction)
204          (sm90InstructionRefused b"an SM86 system form has no measured sm_90 encoding"))))
205
206-- The realization a device region carries for an sm_90 plan: the same for
207-- every program, since it is applied instruction by instruction.
208def sm90LowerRealization : NvidiaDeviceRealization =
209  (constructor NvidiaDeviceRealization NvidiaDeviceRealizedEachInstruction sm90Architecture sm90LowerInstruction)

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.