Source/Packages

Accelerator.SM86.InstructionEncoding

packages/hardware/architectures/nvidia-sm86/src/Accelerator/SM86/InstructionEncoding.alpha

2,937 lines191 declarations105.3 KiBSHA-256 0462dcc8328a

Complete file · line 585

InstructionEncoding.alpha

Definition view

Large source region · 2,937 lines

module Accelerator.SM86.InstructionEncoding

import Accelerator.SM86.FieldEncoding
import Accelerator.SM86.Immediate
import Accelerator.SM86.Instruction
import Accelerator.SM86.NumericSemantics
import Accelerator.SM86.Types
import Std.Byte
import Std.Natural
import Accelerator.SM86.Control

family SM86InstructionEncodingResult : Type 0
constructor SM86InstructionEncodingSucceeded
field unrestricted sm86InstructionEncodedBytes : Bytes
field unrestricted sm86InstructionEncodingTelemetry : SM86FieldEncodingTelemetry
constructor SM86InstructionEncodingFieldFailed
field unrestricted sm86InstructionFieldError : SM86EncodingErrorCode
field unrestricted sm86InstructionFieldPosition : Nat
field unrestricted sm86InstructionFieldWidth : Nat
field unrestricted sm86InstructionFieldDetail : Nat
field unrestricted sm86InstructionFieldTelemetry : SM86FieldEncodingTelemetry
constructor SM86InstructionEncodingUnsupported
field unrestricted sm86UnsupportedInstructionOrdinal : Nat

end-family

family SM86ProgramEncodingTelemetry : Type 0
constructor SM86ProgramEncodingTelemetryValue
field unrestricted sm86ProgramTelemetryInstructions : Nat
field unrestricted sm86ProgramTelemetryBytes : Nat
field unrestricted sm86ProgramTelemetryFields : Nat
field unrestricted sm86ProgramTelemetryBits : Nat
field unrestricted sm86ProgramTelemetryHighestExclusiveBit : Nat

end-family

family SM86ProgramEncodingResult : Type 0
constructor SM86ProgramEncodingSucceeded
field unrestricted sm86ProgramEncodedBytes : Bytes
field unrestricted sm86ProgramEncodingTelemetry : SM86ProgramEncodingTelemetry
constructor SM86ProgramEncodingFailed
field unrestricted sm86ProgramFailureInstructionIndex : Nat
field unrestricted sm86ProgramFailure : SM86InstructionEncodingResult
field unrestricted sm86ProgramFailureTelemetry : SM86ProgramEncodingTelemetry

end-family

-- One Ampere instruction contains 128 bits, including its control fields.
def sm86InstructionBytes : Nat =
  16

def sm86InstructionNaturalOne =
  1

def sm86InstructionNaturalTwo =
  2

def sm86InstructionNaturalThree =
  3

def sm86InstructionNaturalFive =
  5

def sm86InstructionNaturalEight =
  8

def sm86InstructionNaturalSixteen =
  16

def sm86InstructionNaturalTwentyFour =
  24

def sm86InstructionNaturalThirtyTwo =
  32

def sm86InstructionNaturalThirtyEight =
  38

def sm86InstructionNaturalForty =
  40

def sm86InstructionNaturalFiftyFour =
  54

def sm86InstructionNaturalFiftyThree =
  53

def sm86InstructionNaturalFiftyEight =
  58

def sm86InstructionNaturalSixty =
  60

def sm86InstructionNaturalSixtyOne =
  61

def sm86InstructionNaturalSixtyThree =
  63

def sm86InstructionNaturalSixtyFour =
  64

def sm86InstructionNaturalSeventyTwo =
  72

def sm86InstructionNaturalSeventyEight =
  78

def sm86InstructionNaturalEighty =
  80

def sm86InstructionNaturalEightyOne =
  81

def sm86InstructionNaturalEightyFour =
  84

def sm86InstructionNaturalThirteen =
  13

def sm86Opcode =
  (lambda unrestricted high : Byte . unrestricted low : Byte .
    naturalAdd
      (naturalMultiply (byte-to-nat high) byteNaturalTwoHundredFiftySix)
      (byte-to-nat low))

def sm86OpcodeMoveConstant =
  (sm86Opcode 10 2)

def sm86OpcodeMoveImmediate =
  (sm86Opcode 8 2)

def sm86OpcodeSpecialToRegister =
  (sm86Opcode 9 25)

def sm86OpcodeIntegerMultiplyAddImmediate =
  (sm86Opcode 8 36)

def sm86OpcodeIntegerMultiplyAddConstant =
  (sm86Opcode 10 36)

def sm86OpcodeIntegerMultiplyAddWideConstant =
  (sm86Opcode 6 37)

def sm86OpcodeIntegerAddThreeImmediate =
  (sm86Opcode 8 16)

def sm86OpcodeIntegerAddThreeRegister =
  (sm86Opcode 2 16)

def sm86OpcodeShiftRightImmediate =
  (sm86Opcode 8 25)

def sm86OpcodeLogicThreeInputTruthTable =
  (sm86Opcode 2 18)

def sm86OpcodeIntegerToFloat =
  (sm86Opcode 2 69)

def sm86OpcodeFloatPairToPackedHalfPair =
  (sm86Opcode 2 62)

def sm86OpcodeHalfToFloat =
  (sm86Opcode 2 48)

def sm86OpcodeTensorCoreHalfMatrixMultiplyAccumulate16x8x16Float32 =
  (sm86Opcode 2 60)

-- IMMA (0x237)
def sm86OpcodeTensorCoreInteger8MatrixMultiplyAccumulate =
  (sm86Opcode 2 55)

-- PRMT with an immediate selector (the bfloat16 widening)
def sm86OpcodePermuteImmediate =
  (sm86Opcode 8 22)

def sm86OpcodeFloatMinimumOrMaximum =
  (sm86Opcode 2 9)

def sm86OpcodeFloatAdd =
  (sm86Opcode 2 33)

def sm86OpcodeFloatMultiply =
  (sm86Opcode 2 32)

def sm86OpcodeFloatFusedMultiplyAdd =
  (sm86Opcode 2 35)

def sm86OpcodeMultiFunctionUnitApproximation =
  (sm86Opcode 3 8)

def sm86OpcodeFloatNegate =
  (sm86Opcode 2 33)

def sm86OpcodeLoadGlobal =
  (sm86Opcode 9 129)

def sm86OpcodeWarpShuffle =
  (sm86Opcode 15 137)

def sm86OpcodeLoadShared =
  (sm86Opcode 9 132)

def sm86OpcodeLoadSharedMatrix =
  (sm86Opcode 8 59)

def sm86OpcodeStoreShared =
  (sm86Opcode 3 136)

def sm86OpcodeBarrierSynchronize =
  (sm86Opcode 11 29)

def sm86OpcodeLoadGlobalToShared =
  (sm86Opcode 15 174)

def sm86OpcodeCommitAsyncGroup =
  (sm86Opcode 9 175)

def sm86OpcodeWaitAsyncGroups =
  (sm86Opcode 9 26)

def sm86OpcodeStoreGlobal =
  (sm86Opcode 9 134)

def sm86OpcodeReduceGlobalAddFloat32 =
  (sm86Opcode 9 142)

def sm86OpcodePredicateGreaterThanImmediate =
  (sm86Opcode 8 12)

def sm86OpcodeExit =
  (sm86Opcode 9 77)

def sm86OpcodeBranch =
  (sm86Opcode 9 71)

def sm86RegisterNatural =
  (lambda unrestricted register : SM86Register .
    eliminate
      SM86Register
      (lambda unrestricted current : SM86Register . Nat)
      register
      (branch SM86RegisterValue value . byte-to-nat value))

def sm86Unsigned32Natural =
  (lambda unrestricted value : SM86Unsigned32 .
    eliminate
      SM86Unsigned32
      (lambda unrestricted current : SM86Unsigned32 . Nat)
      value
      (branch
        SM86Unsigned32Value
        byte0
        byte1
        byte2
        byte3
        .
        then (naturalAdd (byte-to-nat byte0))
          (naturalMultiply byteNaturalTwoHundredFiftySix)
          (naturalAdd (byte-to-nat byte1))
          (naturalMultiply byteNaturalTwoHundredFiftySix)
          (naturalAdd (byte-to-nat byte2))
          (naturalMultiply byteNaturalTwoHundredFiftySix)
          byte-to-nat
          byte3))

def sm86Unsigned32BytesNatural =
  (lambda unrestricted byte0 : Byte .
    unrestricted byte1 : Byte .
    unrestricted byte2 : Byte .
    unrestricted byte3 : Byte .
    sm86Unsigned32Natural (constructor SM86Unsigned32 SM86Unsigned32Value byte0 byte1 byte2 byte3))

def sm86InstructionField =
  (lambda unrestricted position : Nat . unrestricted width : Nat . unrestricted value : Nat .
    constructor SM86EncodedField SM86EncodedFieldValue position width value)

def sm86InstructionFieldsEmpty =
  SM86EncodedFieldListEmpty

def sm86InstructionFieldsCons =
  (lambda unrestricted field : SM86EncodedField . unrestricted tail : SM86EncodedFieldList .
    constructor SM86EncodedFieldList SM86EncodedFieldListCons field tail)

def sm86InstructionOneField =
  (lambda unrestricted position : Nat . unrestricted width : Nat . unrestricted value : Nat .
    sm86InstructionFieldsCons
      (sm86InstructionField position width value)
      sm86InstructionFieldsEmpty)

def sm86InstructionPrependField =
  (lambda unrestricted position : Nat .
    unrestricted width : Nat .
    unrestricted value : Nat .
    unrestricted tail : SM86EncodedFieldList .
    sm86InstructionFieldsCons (sm86InstructionField position width value) tail)

-- Fixed32 operands retain their four-byte representation through field packing.
def sm86InstructionWord32Field =
  (lambda unrestricted position : Nat . unrestricted value : SM86Unsigned32 .
    constructor SM86EncodedField SM86EncodedFieldWord32Value position value)

def sm86InstructionOneWord32Field =
  (lambda unrestricted position : Nat . unrestricted value : SM86Unsigned32 .
    sm86InstructionFieldsCons
      (sm86InstructionWord32Field position value)
      sm86InstructionFieldsEmpty)

def sm86InstructionPrependWord32Field =
  (lambda unrestricted position : Nat .
    unrestricted value : SM86Unsigned32 .
    unrestricted tail : SM86EncodedFieldList .
    sm86InstructionFieldsCons (sm86InstructionWord32Field position value) tail)

def sm86InstructionWord24Field =
  (lambda unrestricted position : Nat .
    unrestricted byte0 : Byte .
    unrestricted byte1 : Byte .
    unrestricted byte2 : Byte .
    constructor SM86EncodedField SM86EncodedFieldWord24Value position byte0 byte1 byte2)

def sm86InstructionOneWord24Field =
  (lambda unrestricted position : Nat .
    unrestricted byte0 : Byte .
    unrestricted byte1 : Byte .
    unrestricted byte2 : Byte .
    sm86InstructionFieldsCons
      (sm86InstructionWord24Field position byte0 byte1 byte2)
      sm86InstructionFieldsEmpty)

def sm86InstructionPrependWord24Field =
  (lambda unrestricted position : Nat .
    unrestricted byte0 : Byte .
    unrestricted byte1 : Byte .
    unrestricted byte2 : Byte .
    unrestricted tail : SM86EncodedFieldList .
    sm86InstructionFieldsCons (sm86InstructionWord24Field position byte0 byte1 byte2) tail)

def sm86InstructionUnsupported =
  (lambda unrestricted ordinal : Nat .
    constructor SM86InstructionEncodingResult SM86InstructionEncodingUnsupported ordinal)

def sm86InstructionFieldFailure =
  (lambda unrestricted error : SM86EncodingErrorCode .
    unrestricted position : Nat .
    unrestricted width : Nat .
    unrestricted detail : Nat .
    unrestricted telemetry : SM86FieldEncodingTelemetry .
    constructor
      SM86InstructionEncodingResult
      SM86InstructionEncodingFieldFailed
      error
      position
      width
      detail
      telemetry)

def sm86InstructionContinueFields =
  (lambda unrestricted operands : SM86EncodedFieldList .
    unrestricted header : SM86FieldEncodingResult .
    eliminate
      SM86FieldEncodingResult
      (lambda unrestricted current : SM86FieldEncodingResult . SM86InstructionEncodingResult)
      header
      (branch
        SM86FieldEncodingSucceeded
        headerBytes
        headerTelemetry
        .
        eliminate
          SM86FieldEncodingResult
          (lambda unrestricted current : SM86FieldEncodingResult . SM86InstructionEncodingResult)
          (sm86EncodeFieldListFrom operands headerBytes)
          (branch
            SM86FieldEncodingSucceeded
            complete
            operandTelemetry
            .
            constructor
              SM86InstructionEncodingResult
              SM86InstructionEncodingSucceeded
              complete
              (sm86MergeFieldTelemetry headerTelemetry operandTelemetry))
          (branch
            SM86FieldEncodingFailed
            error
            position
            width
            detail
            telemetry
            .
            sm86InstructionFieldFailure
              error
              position
              width
              detail
              (sm86MergeFieldTelemetry headerTelemetry telemetry)))
      (branch
        SM86FieldEncodingFailed
        error
        position
        width
        detail
        telemetry
        .
        sm86InstructionFieldFailure error position width detail telemetry))

def sm86EncodeInstructionFields =
  (lambda unrestricted opcode : Nat .
    unrestricted guard : SM86InstructionGuard .
    unrestricted control : SM86Control .
    unrestricted operands : SM86EncodedFieldList .
    sm86InstructionContinueFields operands (sm86EncodeInstructionHeader opcode guard control))

def sm86SpecialRegisterNatural =
  (lambda unrestricted special : SM86SpecialRegister .
    eliminate
      SM86SpecialRegister
      (lambda unrestricted current : SM86SpecialRegister . Nat)
      special
      (branch SM86CooperativeThreadArrayIdX . 37)
      (branch SM86CooperativeThreadArrayIdY . 38)
      (branch SM86CooperativeThreadArrayIdZ . 39)
      (branch SM86ThreadIdX . 33)
      (branch SM86ClockLow . 80)
      (branch SM86GlobalTimerLow . 82)
      (branch SM86GlobalTimerHigh . 83))

def sm86ShuffleModeNatural =
  (lambda unrestricted mode : SM86ShuffleMode .
    eliminate
      SM86ShuffleMode
      (lambda unrestricted current : SM86ShuffleMode . Nat)
      mode
      (branch SM86ShuffleIndex . 0)
      (branch SM86ShuffleUp . 1)
      (branch SM86ShuffleDown . 2)
      (branch SM86ShuffleButterfly . 3))

def sm86EncodeMoveConstant =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted bank : Byte .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeMoveConstant guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyEight
        sm86InstructionNaturalSixteen
        (sm86Unsigned32Natural offset))
      (sm86InstructionPrependField
        sm86InstructionNaturalFiftyFour
        sm86InstructionNaturalFive
        (byte-to-nat bank))
      (sm86InstructionPrependField sm86InstructionNaturalSixtyFour sm86InstructionNaturalEight 0)
      (sm86InstructionOneField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight)
      15)

def sm86EncodeMoveImmediate =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted immediate : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeMoveImmediate guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependWord32Field sm86InstructionNaturalThirtyTwo immediate)
      (sm86InstructionOneField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight)
      15)

def sm86EncodeSpecialToRegister =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted special : SM86SpecialRegister .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeSpecialToRegister guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionOneField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight)
      sm86SpecialRegisterNatural
      special)

def sm86EncodeIntegerMultiplyAddImmediate =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted immediate : SM86Unsigned32 .
    unrestricted addend : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeIntegerMultiplyAddImmediate guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural left))
      (sm86InstructionPrependWord32Field sm86InstructionNaturalThirtyTwo immediate)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixtyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural addend))
      (sm86InstructionOneWord24Field sm86InstructionNaturalSeventyTwo 2 142)
      7)

def sm86EncodeIntegerMultiplyAddConstant =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted bank : Byte .
    unrestricted offset : SM86Unsigned32 .
    unrestricted addend : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeIntegerMultiplyAddConstant guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural left))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyEight
        sm86InstructionNaturalSixteen
        (sm86Unsigned32Natural offset))
      (sm86InstructionPrependField
        sm86InstructionNaturalFiftyFour
        sm86InstructionNaturalFive
        (byte-to-nat bank))
      (sm86InstructionPrependField
        sm86InstructionNaturalSixtyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural addend))
      (sm86InstructionOneWord24Field sm86InstructionNaturalSeventyTwo 2 142)
      7)

def sm86EncodeIntegerMultiplyAddWideConstant =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted right : SM86Register .
    unrestricted bank : Byte .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    nat-eliminate
      (lambda unrestricted destinationIsZero : Nat . SM86InstructionEncodingResult)
      (then (sm86EncodeInstructionFields sm86OpcodeIntegerMultiplyAddWideConstant guard control)
        (sm86InstructionPrependField
          sm86InstructionNaturalSixteen
          sm86InstructionNaturalEight
          (sm86RegisterNatural destination))
        (sm86InstructionPrependField
          sm86InstructionNaturalTwentyFour
          sm86InstructionNaturalEight
          (sm86RegisterNatural left))
        (sm86InstructionPrependField
          sm86InstructionNaturalThirtyEight
          sm86InstructionNaturalSixteen
          (sm86Unsigned32Natural offset))
        (sm86InstructionPrependField
          sm86InstructionNaturalFiftyFour
          sm86InstructionNaturalFive
          (byte-to-nat bank))
        (sm86InstructionPrependField
          sm86InstructionNaturalSixtyFour
          sm86InstructionNaturalEight
          (sm86RegisterNatural right))
        (sm86InstructionOneWord24Field sm86InstructionNaturalSeventyTwo 0 142)
        7)
      (lambda unrestricted invalidPredecessor : Nat .
        unrestricted invalidInduction : SM86InstructionEncodingResult .
        sm86InstructionUnsupported 6)
      (pipe destination sm86RegisterNatural nat-to-byte (byte-equal 255)))

def sm86EncodeIntegerAdd3Immediate =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted immediate : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeIntegerAddThreeImmediate guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural left))
      (sm86InstructionPrependWord32Field sm86InstructionNaturalThirtyTwo immediate)
      (sm86InstructionPrependField sm86InstructionNaturalSixtyFour sm86InstructionNaturalEight 255)
      (sm86InstructionPrependField sm86InstructionNaturalEightyOne sm86InstructionNaturalThree 7)
      (sm86InstructionOneField sm86InstructionNaturalEightyFour sm86InstructionNaturalThree)
      7)

def sm86EncodeIntegerAdd3Register =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted right : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeIntegerAddThreeRegister guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural left))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural right))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 255 224 255)
      7)

def sm86EncodeShiftRightImmediate =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted source : SM86Register .
    unrestricted amount : Byte .
    unrestricted control : SM86Control .
    nat-eliminate
      (lambda unrestricted amountInvalid : Nat . SM86InstructionEncodingResult)
      (then (sm86EncodeInstructionFields sm86OpcodeShiftRightImmediate guard control)
        (sm86InstructionPrependField
          sm86InstructionNaturalSixteen
          sm86InstructionNaturalEight
          (sm86RegisterNatural destination))
        (sm86InstructionPrependField
          sm86InstructionNaturalTwentyFour
          sm86InstructionNaturalEight
          255)
        (sm86InstructionPrependField
          sm86InstructionNaturalThirtyTwo
          sm86InstructionNaturalThirtyTwo
          (byte-to-nat amount))
        (sm86InstructionPrependField
          sm86InstructionNaturalSixtyFour
          sm86InstructionNaturalEight
          (sm86RegisterNatural source))
        (sm86InstructionOneWord24Field sm86InstructionNaturalSeventyTwo 22 1)
        0)
      (lambda unrestricted invalidPredecessor : Nat .
        unrestricted invalidInduction : SM86InstructionEncodingResult .
        sm86InstructionUnsupported 9)
      (nat-less-than 31 (byte-to-nat amount)))

def sm86EncodeLogic3 =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted right : SM86Register .
    unrestricted truthTable : Byte .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeLogicThreeInputTruthTable guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural left))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural right))
      (sm86InstructionPrependField sm86InstructionNaturalSixtyFour sm86InstructionNaturalEight 255)
      (sm86InstructionPrependField
        sm86InstructionNaturalSeventyTwo
        sm86InstructionNaturalEight
        (byte-to-nat truthTable))
      (sm86InstructionOneField sm86InstructionNaturalEighty sm86InstructionNaturalSixteen)
      (sm86Unsigned32BytesNatural 142 7 0)
      0)

-- Register-form DADD/DMUL, with RNE and no implicit contraction. Source facts:
-- Mesa NAK sm70_encode.rs at 61a2e907f1d6e4a8432d91d0fde5d439a875fbbe,
-- OpDAdd/OpDMul and encode_alu_base (upstream mirror):
-- https://chromium.googlesource.com/external/gitlab.freedesktop.org/mesa/mesa/+/61a2e907f1d6e4a8432d91d0fde5d439a875fbbe/src/nouveau/compiler/nak/sm70_encode.rs
-- Addition's right operand occupies src2; multiplication uses src1. Subtraction
-- negates src2. These encodings still require separate physical qualification.
def sm86EncodeFloat64Binary =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted operation : SM86Float64Operation .
    unrestricted destination : SM86RegisterPair .
    unrestricted left : SM86RegisterPair .
    unrestricted right : SM86RegisterPair .
    unrestricted control : SM86Control .
    let unrestricted encode =
      (lambda unrestricted opcode : Nat . unrestricted rightAt : Nat . unrestricted negate : Nat .
        then (sm86EncodeInstructionFields opcode guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterPairIndex destination))
          (sm86InstructionPrependField 24 8 (sm86RegisterPairIndex left))
          (sm86InstructionPrependField rightAt 8 (sm86RegisterPairIndex right))
          (sm86InstructionOneField 75 1)
          negate)
      in
      nat-eliminate
        (lambda unrestricted valid : Nat . SM86InstructionEncodingResult)
        (sm86InstructionUnsupported 64)
        (lambda unrestricted predecessor : Nat .
          unrestricted ignored : SM86InstructionEncodingResult .
          eliminate
            SM86Float64Operation
            (lambda unrestricted value : SM86Float64Operation . SM86InstructionEncodingResult)
            operation
            (branch SM86Float64Add . encode 0x229 64 0)
            (branch SM86Float64Subtract . encode 0x229 64 1)
            (branch SM86Float64Multiply . encode 0x228 32 0))
        (sm86Float64RegistersAdmitted destination left right))

-- F2F register form, src at bits 32..39, width codes log2(bytes),
-- RNE at 78..79 and FTZ at 80 both zero. Source: OpF2F and encode_alu_base,
-- Mesa revision 61a2e907f1d6e4a8432d91d0fde5d439a875fbbe (link above).
def sm86EncodeFloatConvert =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted conversion : SM86FloatConversion .
    unrestricted control : SM86Control .
    nat-eliminate
      (lambda unrestricted valid : Nat . SM86InstructionEncodingResult)
      (sm86InstructionUnsupported 65)
      (lambda unrestricted predecessor : Nat .
        unrestricted ignored : SM86InstructionEncodingResult .
        then (sm86EncodeInstructionFields 0x310 guard control)
          (sm86InstructionPrependField
            16
            8
            (sm86RegisterNatural (sm86FloatConversionDestination conversion)))
          (sm86InstructionPrependField
            32
            8
            (sm86RegisterNatural (sm86FloatConversionSource conversion)))
          (sm86InstructionPrependField 75 2 (sm86FloatConversionDestinationSize conversion))
          (sm86InstructionOneField 84 2)
          sm86FloatConversionSourceSize
          conversion)
      (sm86FloatConversionAdmitted conversion))

def sm86EncodeFloatMultiply =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted right : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeFloatMultiply guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural left))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural right))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 0 0 64)
      0)

def sm86EncodeIntegerToFloat =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted source : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeIntegerToFloat guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural source))
      (sm86InstructionPrependField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight 20)
      (sm86InstructionOneField sm86InstructionNaturalEighty sm86InstructionNaturalEight)
      32)

def sm86HalfSelectorNatural =
  (lambda unrestricted selector : SM86HalfSelector .
    eliminate
      SM86HalfSelector
      (lambda unrestricted current : SM86HalfSelector . Nat)
      selector
      (branch SM86LowHalf . 0)
      (branch SM86HighHalf . sm86InstructionNaturalOne))

def sm86EncodeFloat32PairToHalf2 =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted sourceHigh : SM86Register .
    unrestricted sourceLow : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeFloatPairToPackedHalfPair guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural sourceHigh))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural sourceLow))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 255 0 0)
      0)

def sm86EncodeHalfToFloat =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted source : SM86Register .
    unrestricted selector : SM86HalfSelector .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeHalfToFloat guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField sm86InstructionNaturalTwentyFour sm86InstructionNaturalEight 255)
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural source))
      (sm86InstructionPrependField
        sm86InstructionNaturalSixty
        sm86InstructionNaturalOne
        (sm86HalfSelectorNatural selector))
      (sm86InstructionPrependField
        sm86InstructionNaturalSixtyOne
        sm86InstructionNaturalOne
        sm86InstructionNaturalOne)
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 0 65 0)
      0)

-- F2FP.SATFINITE.{E4M3,E5M2}.F32.PACK_AB_MERGE_C: the merge register at
-- bit 64, its half at bit 72 (1: H1), bit 76 the format (1: E4M3, 0: E5M2),
-- bits 77, 78, 87 and 90 set (ptxas 12.9 for sm_90a through Triton,
-- Inductor's quantization kernels: 0x...723e / 0x04807108 for E4M3 with
-- merge R8.H1, 0x0480610a for E5M2 with R10.H1)
def sm86Float8FormatNatural =
  (lambda unrestricted format : SM86Float8Format .
    eliminate
      SM86Float8Format
      (lambda unrestricted current : SM86Float8Format . Nat)
      format
      (branch SM86Float8E4M3 . 1)
      (branch SM86Float8E5M2 . 0))

-- FADD / FMUL / FFMA with modifiers (ptxas 12.9 for sm_90a, register
-- forms; Proof.FloatArithmeticContract holds both encoders to its words):
-- the opcode 0x221 / 0x220 / 0x223 (FMUL with bit 86 set, as plain FMUL),
-- the addend at bit 64 (FFMA), |right| bit 62, -right bit 63, -left bit 72,
-- |left| bit 73, -addend bit 75, .SAT bit 77, the rounding bits 78..79 (RN
-- 0, RM 1, RP 2, RZ 3), .FTZ bit 80
def sm86FloatOperationOpcode =
  (lambda unrestricted operation : SM86FloatOperation .
    eliminate
      SM86FloatOperation
      (lambda unrestricted current : SM86FloatOperation . Nat)
      operation
      (branch SM86FloatOperationAdd . sm86OpcodeFloatAdd)
      (branch SM86FloatOperationMultiply . sm86OpcodeFloatMultiply)
      (branch SM86FloatOperationFusedMultiplyAdd . sm86OpcodeFloatFusedMultiplyAdd))

-- 1 for FFMA (its addend encoded), FMUL (bit 86)
def sm86FloatOperationFused =
  (lambda unrestricted operation : SM86FloatOperation .
    eliminate
      SM86FloatOperation
      (lambda unrestricted current : SM86FloatOperation . Nat)
      operation
      (branch SM86FloatOperationAdd . 0)
      (branch SM86FloatOperationMultiply . 0)
      (branch SM86FloatOperationFusedMultiplyAdd . 1))

def sm86FloatOperationMultiplies =
  (lambda unrestricted operation : SM86FloatOperation .
    eliminate
      SM86FloatOperation
      (lambda unrestricted current : SM86FloatOperation . Nat)
      operation
      (branch SM86FloatOperationAdd . 0)
      (branch SM86FloatOperationMultiply . 1)
      (branch SM86FloatOperationFusedMultiplyAdd . 0))

def sm86RoundingNatural =
  (lambda unrestricted rounding : SM86Rounding .
    eliminate
      SM86Rounding
      (lambda unrestricted current : SM86Rounding . Nat)
      rounding
      (branch SM86RoundNearestEven . 0)
      (branch SM86RoundDown . 1)
      (branch SM86RoundUp . 2)
      (branch SM86RoundTowardZero . 3))

def sm86EncodeFloatArithmetic =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted operation : SM86FloatOperation .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted right : SM86Register .
    unrestricted addend : SM86Register .
    unrestricted modifiers : SM86FloatModifiers .
    unrestricted control : SM86Control .
    eliminate
      SM86FloatModifiers
      (lambda unrestricted current : SM86FloatModifiers . SM86InstructionEncodingResult)
      modifiers
      (branch
        SM86FloatModifiersValue
        rounding
        flush
        saturate
        negateLeft
        absoluteLeft
        negateRight
        absoluteRight
        negateAddend
        .
        then (sm86EncodeInstructionFields (sm86FloatOperationOpcode operation) guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
          (sm86InstructionPrependField 24 8 (sm86RegisterNatural left))
          (sm86InstructionPrependField 32 8 (sm86RegisterNatural right))
          (sm86InstructionPrependField 62 1 absoluteRight)
          (sm86InstructionPrependField 63 1 negateRight)
          (sm86InstructionPrependField
            64
            8
            (naturalSelect (sm86FloatOperationFused operation) (sm86RegisterNatural addend) 0))
          (sm86InstructionPrependField 72 1 negateLeft)
          (sm86InstructionPrependField 73 1 absoluteLeft)
          (sm86InstructionPrependField 75 1 negateAddend)
          (sm86InstructionPrependField 77 1 saturate)
          (sm86InstructionPrependField 78 2 (sm86RoundingNatural rounding))
          (sm86InstructionPrependField 80 1 flush)
          (sm86InstructionOneField 86 1)
          sm86FloatOperationMultiplies
          operation))

def sm86EncodeFloat32PairToFloat8Pair =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted sourceHigh : SM86Register .
    unrestricted sourceLow : SM86Register .
    unrestricted merge : SM86Register .
    unrestricted mergeHalf : SM86HalfSelector .
    unrestricted format : SM86Float8Format .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeFloatPairToPackedHalfPair guard control)
      (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
      (sm86InstructionPrependField 24 8 (sm86RegisterNatural sourceHigh))
      (sm86InstructionPrependField 32 8 (sm86RegisterNatural sourceLow))
      (sm86InstructionPrependField 64 8 (sm86RegisterNatural merge))
      (sm86InstructionPrependField 72 1 (sm86HalfSelectorNatural mergeHalf))
      (sm86InstructionPrependField 76 1 (sm86Float8FormatNatural format))
      (sm86InstructionPrependField 77 2 3)
      (sm86InstructionPrependField 87 1 1)
      (sm86InstructionOneField 90 1)
      1)

-- F2FP.BF16.PACK_AB: F2FP.PACK_AB with bit 76 set (the bfloat16 result;
-- ptxas's encoding for sm_86 and sm_121, 2026-09-25)
def sm86EncodeFloat32PairToBFloat16Pair =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted sourceHigh : SM86Register .
    unrestricted sourceLow : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeFloatPairToPackedHalfPair guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural sourceHigh))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural sourceLow))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 255 16 0)
      0)

-- PRMT Rd, Ra, selector, RZ: the result's bytes, high to low, are Ra's
-- bytes 1, 0 (H0) or 3, 2 (H1) then two of RZ's
def sm86BFloat16WidenSelector =
  (lambda unrestricted selector : SM86HalfSelector .
    eliminate
      SM86HalfSelector
      (lambda unrestricted current : SM86HalfSelector . SM86Unsigned32)
      selector
      (branch SM86LowHalf . constructor SM86Unsigned32 SM86Unsigned32Value 68 16 0 0)
      (branch SM86HighHalf . constructor SM86Unsigned32 SM86Unsigned32Value 68 50 0 0))

def sm86EncodeBFloat16ToFloat =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted source : SM86Register .
    unrestricted selector : SM86HalfSelector .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodePermuteImmediate guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural source))
      (sm86InstructionPrependWord32Field
        sm86InstructionNaturalThirtyTwo
        (sm86BFloat16WidenSelector selector))
      (sm86InstructionOneField sm86InstructionNaturalSixtyFour sm86InstructionNaturalEight)
      255)

-- HMMA.16816.F32.BF16: the binary16 tile product's encoding with bit 82 set
def sm86EncodeHMMA16816F32BF16 =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted fragmentA : SM86Register .
    unrestricted fragmentB : SM86Register .
    unrestricted accumulator : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeTensorCoreHalfMatrixMultiplyAccumulate16x8x16Float32 guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural fragmentA))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural fragmentB))
      (sm86InstructionPrependField
        sm86InstructionNaturalSixtyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural accumulator))
      (sm86InstructionPrependField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight 24)
      (sm86InstructionOneField sm86InstructionNaturalEighty sm86InstructionNaturalEight)
      4)

def sm86EncodeHMMA16816F32F16 =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted fragmentA : SM86Register .
    unrestricted fragmentB : SM86Register .
    unrestricted accumulator : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeTensorCoreHalfMatrixMultiplyAccumulate16x8x16Float32 guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural fragmentA))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural fragmentB))
      (sm86InstructionPrependField
        sm86InstructionNaturalSixtyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural accumulator))
      (sm86InstructionOneField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight)
      24)

-- HMMA.16816.F16: the f32-accumulate form's fields with 8, not 24, at bit
-- 72 (ptxas: 0x...081a against 0x...1814)
def sm86EncodeHMMA16816F16F16 =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted fragmentA : SM86Register .
    unrestricted fragmentB : SM86Register .
    unrestricted accumulator : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeTensorCoreHalfMatrixMultiplyAccumulate16x8x16Float32 guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural fragmentA))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural fragmentB))
      (sm86InstructionPrependField
        sm86InstructionNaturalSixtyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural accumulator))
      (sm86InstructionOneField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight)
      8)

-- IMMA.16832.S8.S8: opcode 0x237, the same register fields, 0x5c at bit 72
-- and 0x40 at bit 80 (ptxas: 0x...405c10)
def sm86EncodeIMMA16832S8S8 =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted fragmentA : SM86Register .
    unrestricted fragmentB : SM86Register .
    unrestricted accumulator : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeTensorCoreInteger8MatrixMultiplyAccumulate guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural fragmentA))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural fragmentB))
      (sm86InstructionPrependField
        sm86InstructionNaturalSixtyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural accumulator))
      (sm86InstructionPrependField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight 92)
      (sm86InstructionOneField sm86InstructionNaturalEighty sm86InstructionNaturalEight)
      64)

def sm86EncodeFloatAdd =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted right : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeFloatAdd guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural left))
      (sm86InstructionOneField sm86InstructionNaturalThirtyTwo sm86InstructionNaturalEight)
      sm86RegisterNatural
      right)

def sm86EncodeFloatFusedMultiplyAdd =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted right : SM86Register .
    unrestricted addend : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeFloatFusedMultiplyAdd guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural left))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural right))
      (sm86InstructionOneField sm86InstructionNaturalSixtyFour sm86InstructionNaturalEight)
      sm86RegisterNatural
      addend)

def sm86EncodeMultiFunction =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted source : SM86Register .
    unrestricted operation : SM86MultiFunction .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeMultiFunctionUnitApproximation guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural source))
      (sm86InstructionOneField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight)
      byte-to-nat
      sm86MultiFunctionSelector
      operation)

def sm86FloatExtremumDescriptorNatural =
  (lambda unrestricted extremum : SM86FloatExtremum .
    eliminate
      SM86FloatExtremum
      (lambda unrestricted current : SM86FloatExtremum . Nat)
      extremum
      (branch SM86FloatMinimum . sm86Unsigned32BytesNatural 0 0 128 3)
      (branch SM86FloatMaximum . sm86Unsigned32BytesNatural 0 0 128 7))

-- Keep the public Natural descriptor; encoding consumes its bounded sibling.
def sm86FloatExtremumDescriptorWord32 =
  (lambda unrestricted extremum : SM86FloatExtremum .
    eliminate
      SM86FloatExtremum
      (lambda unrestricted current : SM86FloatExtremum . SM86Unsigned32)
      extremum
      (branch SM86FloatMinimum . constructor SM86Unsigned32 SM86Unsigned32Value 0 0 128 3)
      (branch SM86FloatMaximum . constructor SM86Unsigned32 SM86Unsigned32Value 0 0 128 7))

def sm86EncodeFloatExtremum =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted left : SM86Register .
    unrestricted right : SM86Register .
    unrestricted extremum : SM86FloatExtremum .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeFloatMinimumOrMaximum guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural left))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural right))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      sm86FloatExtremumDescriptorWord32
      extremum)

def sm86EncodeFloatNegate =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted source : SM86Register .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeFloatNegate guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural source))
      (sm86InstructionPrependField sm86InstructionNaturalThirtyTwo sm86InstructionNaturalEight 255)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixtyThree
        sm86InstructionNaturalOne
        sm86InstructionNaturalOne)
      (sm86InstructionOneField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalOne)
      sm86InstructionNaturalOne)

-- A global load or store's immediate byte offset (bits 40..63) is signed:
-- the card reads 2^23 and above as negative. Positive offsets are encoded
-- in the low 23 bits, so a larger one is refused as an overflow instead of
-- addressing memory before its base (offsets here are never negative).
def sm86GlobalOffsetFieldBits : Nat =
  23

def sm86EncodeLoadGlobal =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted address : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeLoadGlobal guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural address))
      (sm86InstructionPrependField sm86InstructionNaturalThirtyTwo sm86InstructionNaturalEight 4)
      (sm86InstructionPrependField
        sm86InstructionNaturalForty
        sm86GlobalOffsetFieldBits
        (sm86Unsigned32Natural offset))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 0 25 30)
      12)

def sm86EncodeLoadGlobalWide =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted address : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeLoadGlobal guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural address))
      (sm86InstructionPrependField sm86InstructionNaturalThirtyTwo sm86InstructionNaturalEight 4)
      (sm86InstructionPrependField
        sm86InstructionNaturalForty
        sm86GlobalOffsetFieldBits
        (sm86Unsigned32Natural offset))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 0 29 30)
      12)

def sm86EncodeShuffle =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted source : SM86Register .
    unrestricted lane : Byte .
    unrestricted segment : SM86Unsigned32 .
    unrestricted mode : SM86ShuffleMode .
    unrestricted control : SM86Control .
    nat-eliminate
      (lambda unrestricted laneInvalid : Nat . SM86InstructionEncodingResult)
      (then (sm86EncodeInstructionFields sm86OpcodeWarpShuffle guard control)
        (sm86InstructionPrependField
          sm86InstructionNaturalSixteen
          sm86InstructionNaturalEight
          (sm86RegisterNatural destination))
        (sm86InstructionPrependField
          sm86InstructionNaturalTwentyFour
          sm86InstructionNaturalEight
          (sm86RegisterNatural source))
        (sm86InstructionPrependField
          sm86InstructionNaturalForty
          sm86InstructionNaturalThirteen
          (sm86Unsigned32Natural segment))
        (sm86InstructionPrependField
          sm86InstructionNaturalFiftyThree
          sm86InstructionNaturalFive
          (byte-to-nat lane))
        (sm86InstructionPrependField
          sm86InstructionNaturalFiftyEight
          sm86InstructionNaturalTwo
          (sm86ShuffleModeNatural mode))
        (sm86InstructionOneField sm86InstructionNaturalEightyOne sm86InstructionNaturalThree)
        7)
      (lambda unrestricted invalidPredecessor : Nat .
        unrestricted invalidInduction : SM86InstructionEncodingResult .
        sm86InstructionUnsupported 20)
      (nat-less-than 31 (byte-to-nat lane)))

def sm86EncodeLoadShared =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted address : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeLoadShared guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural address))
      (sm86InstructionPrependField
        sm86InstructionNaturalForty
        sm86InstructionNaturalTwentyFour
        (sm86Unsigned32Natural offset))
      (sm86InstructionOneField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight)
      72)

def sm86SharedMatrixCountNatural =
  (lambda unrestricted count : SM86SharedMatrixCount .
    eliminate
      SM86SharedMatrixCount
      (lambda unrestricted current : SM86SharedMatrixCount . Nat)
      count
      (branch SM86SharedMatrix1 . 0)
      (branch SM86SharedMatrix2 . sm86InstructionNaturalOne)
      (branch SM86SharedMatrix4 . sm86InstructionNaturalTwo))

def sm86SharedMatrixTransposeNatural =
  (lambda unrestricted transpose : SM86SharedMatrixTranspose .
    eliminate
      SM86SharedMatrixTranspose
      (lambda unrestricted current : SM86SharedMatrixTranspose . Nat)
      transpose
      (branch SM86SharedMatrixNotTransposed . 0)
      (branch SM86SharedMatrixTransposed . sm86InstructionNaturalOne))

def sm86EncodeLoadSharedMatrix =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted destination : SM86Register .
    unrestricted address : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted count : SM86SharedMatrixCount .
    unrestricted transpose : SM86SharedMatrixTranspose .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeLoadSharedMatrix guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural destination))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural address))
      (sm86InstructionPrependField
        sm86InstructionNaturalForty
        sm86InstructionNaturalTwentyFour
        (sm86Unsigned32Natural offset))
      (sm86InstructionPrependField
        sm86InstructionNaturalSeventyTwo
        sm86InstructionNaturalTwo
        (sm86SharedMatrixCountNatural count))
      (sm86InstructionOneField sm86InstructionNaturalSeventyEight sm86InstructionNaturalOne)
      sm86SharedMatrixTransposeNatural
      transpose)

def sm86EncodeStoreShared =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted address : SM86Register .
    unrestricted value : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeStoreShared guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural address))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural value))
      (sm86InstructionPrependField
        sm86InstructionNaturalForty
        sm86InstructionNaturalTwentyFour
        (sm86Unsigned32Natural offset))
      (sm86InstructionOneField sm86InstructionNaturalSeventyTwo sm86InstructionNaturalEight)
      72)

-- LDGSTS.E.BYPASS.128 [address + offset], [source.64 + sourceOffset], as
-- ptxas encodes cp.async.cg ... 16 for sm_86 (nvdisasm -hex on the DGX
-- Spark, 2026-09-26; research p4-hmma/cp_async*.cu): the shared address
-- register at 16, the global pair at 24, the global offset at 32 (signed 12
-- bits in the silicon; this encoder admits 0..0x7ff, bit 43 clear), the
-- shared offset at 44 (20 bits), and bits 64..95 0x0b901c44: BYPASS (bit 81
-- clear), the global-memory descriptor UR4 with its enable (0x44) -- the
-- descriptor LDG's bits 32..39 name too.  The compiler's native encoder
-- (RuntimeMachine) is its twin.
def sm86EncodeLoadGlobalToShared =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted address : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted source : SM86Register .
    unrestricted sourceOffset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeLoadGlobalToShared guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalSixteen
        sm86InstructionNaturalEight
        (sm86RegisterNatural address))
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural source))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        11
        (sm86Unsigned32Natural sourceOffset))
      (sm86InstructionPrependField 44 20 (sm86Unsigned32Natural offset))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 68 28 144)
      11)

-- LDGDEPBAR: no operand; its control's write barrier (SB0) counts the group.
def sm86EncodeCommitAsyncGroup =
  (lambda unrestricted guard : SM86InstructionGuard . unrestricted control : SM86Control .
    sm86EncodeInstructionFields
      sm86OpcodeCommitAsyncGroup
      guard
      control
      sm86InstructionFieldsEmpty)

-- DEPBAR.LE SB0, count: the count at 38 (6 bits), the scoreboard at 44 (SB0),
-- bit 47 the LE form (ptxas's cp.async.wait_group, as above).
def sm86EncodeWaitAsyncGroups =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted count : Byte .
    unrestricted control : SM86Control .
    sm86EncodeInstructionFields
      sm86OpcodeWaitAsyncGroups
      guard
      control
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyEight
        6
        (byte-to-nat count)
        (sm86InstructionPrependField 47 sm86InstructionNaturalOne 1 sm86InstructionFieldsEmpty)))

-- BAR.SYNC.DEFER_BLOCKING 0x0: bit 80 set, as ptxas encodes bar.sync for
-- sm_86 and sm_121 (read off nvdisasm -hex on the DGX Spark, 2026-09-26:
-- high word 0x...0001_0000); without it nvdisasm reads plain BAR.SYNC.  The
-- compiler's native encoder (RuntimeMachine) is its twin.
def sm86EncodeBarrierSynchronize =
  (lambda unrestricted guard : SM86InstructionGuard . unrestricted control : SM86Control .
    sm86EncodeInstructionFields
      sm86OpcodeBarrierSynchronize
      guard
      control
      (sm86InstructionPrependField
        sm86InstructionNaturalEighty
        sm86InstructionNaturalOne
        1
        sm86InstructionFieldsEmpty))

-- BAR.SYNC.DEFER_BLOCKING b, n: BAR.SYNC 0x0's word with the barrier in
-- bits 54..57 and the thread count in bits 42..53 (read off nvdisasm -hex of
-- ptxas sm_86 code on the RTX 3070 pod, 2026-10-09: bar.sync 1, 128 is
-- 0x0042000000007b1d, bar.sync 15, 96 0x03c1800000007b1d; count 0 is the
-- whole CTA, bar.sync 0's word). RuntimeMachine's native encoder is its twin.
def sm86EncodeBarrierSynchronizeCount =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted barrier : Byte .
    unrestricted threads : Nat .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeBarrierSynchronize guard control)
      (sm86InstructionPrependField 42 12 threads)
      (sm86InstructionPrependField sm86InstructionNaturalFiftyFour 4 (byte-to-nat barrier))
      (sm86InstructionPrependField sm86InstructionNaturalEighty sm86InstructionNaturalOne 1)
      sm86InstructionFieldsEmpty)

def sm86EncodeStoreGlobal =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted address : SM86Register .
    unrestricted value : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeStoreGlobal guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural address))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural value))
      (sm86InstructionPrependField
        sm86InstructionNaturalForty
        sm86GlobalOffsetFieldBits
        (sm86Unsigned32Natural offset))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 4 25 16)
      12)

def sm86EncodeStoreGlobalWide =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted address : SM86Register .
    unrestricted value : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeStoreGlobal guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural address))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural value))
      (sm86InstructionPrependField
        sm86InstructionNaturalForty
        sm86GlobalOffsetFieldBits
        (sm86Unsigned32Natural offset))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 4 29 16)
      12)

-- STG.E.64: the register pair [value, value+1] to [address + offset]. The
-- descriptor word differs from the 32-bit store only in the size code (byte
-- 25 -> 27; the 128-bit store is 29), the same three codes the wide-memory
-- descriptors carry. An mma.m16n8k16 fp32 accumulator fragment hands each
-- thread PAIRS of adjacent columns, so a row-major epilogue needs exactly this
-- width: a 128-bit store there overlaps the neighbouring thread and lands on an
-- 8-byte-aligned address, which the SM faults instead of executing.
def sm86EncodeStoreGlobal64 =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted address : SM86Register .
    unrestricted value : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeStoreGlobal guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural address))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural value))
      (sm86InstructionPrependField
        sm86InstructionNaturalForty
        sm86GlobalOffsetFieldBits
        (sm86Unsigned32Natural offset))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 4 27 16)
      12)

def sm86EncodeReduceGlobalAddFloat32 =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted address : SM86Register .
    unrestricted value : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields sm86OpcodeReduceGlobalAddFloat32 guard control)
      (sm86InstructionPrependField
        sm86InstructionNaturalTwentyFour
        sm86InstructionNaturalEight
        (sm86RegisterNatural address))
      (sm86InstructionPrependField
        sm86InstructionNaturalThirtyTwo
        sm86InstructionNaturalEight
        (sm86RegisterNatural value))
      (sm86InstructionPrependField
        sm86InstructionNaturalForty
        sm86InstructionNaturalTwentyFour
        (sm86Unsigned32Natural offset))
      (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour)
      (constructor SM86Unsigned32 SM86Unsigned32Value 132 231 16)
      12)

def sm86EncodePredicateGreater =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted predicate : SM86Predicate .
    unrestricted source : SM86Register .
    unrestricted immediate : SM86Unsigned32 .
    unrestricted control : SM86Control .
    app
      (lambda unrestricted predicateControl : SM86Control .
        then (sm86EncodeInstructionFields sm86OpcodePredicateGreaterThanImmediate guard predicateControl)
          (sm86InstructionPrependField
            sm86InstructionNaturalTwentyFour
            sm86InstructionNaturalEight
            (sm86RegisterNatural source))
          (sm86InstructionPrependWord32Field sm86InstructionNaturalThirtyTwo immediate)
          (sm86InstructionPrependWord32Field
            sm86InstructionNaturalSixtyFour
            (constructor SM86Unsigned32 SM86Unsigned32Value 112 64 240 3))
          (sm86InstructionOneField sm86InstructionNaturalEightyOne sm86InstructionNaturalThree)
          byte-to-nat
          sm86PredicateNumber
          predicate)
      (eliminate
        SM86Control
        (lambda unrestricted current : SM86Control . SM86Control)
        control
        (branch
          SM86ControlValue
          stall
          yieldMode
          writeBarrier
          readBarrier
          waitMask
          reuseMask
          .
          constructor
            SM86Control
            SM86ControlValue
            15
            yieldMode
            writeBarrier
            readBarrier
            waitMask
            reuseMask)))

def sm86EncodeExit =
  (lambda unrestricted guard : SM86InstructionGuard . unrestricted control : SM86Control .
    sm86EncodeInstructionFields
      sm86OpcodeExit
      guard
      control
      (sm86InstructionOneWord32Field
        sm86InstructionNaturalSixtyFour
        (constructor SM86Unsigned32 SM86Unsigned32Value 0 0 128 3)))

-- BRA (relative branch, opcode 0x947): the loop back-edge. Field layout mirrors the golden
-- reference (offset@32(32) = relative BYTE offset from PC+16, descriptor@64(32) = 0x03800000 for a
-- forward branch, 0x0383ffff for a backward branch). The guard predicate (@12) and negate bit (@15)
-- and the control word (@105) are laid down by the shared instruction header, so `@!P0 BRA` reuses the
-- SAME predicate machinery as every predicated instruction. This is a real field-packing encoder (a
-- COMPONENT), never a golden-byte substitute; nvdisasm-verified on the SM86 profile.
def sm86EncodeBranch =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted offset : SM86Unsigned32 .
    unrestricted descriptor : SM86Unsigned32 .
    unrestricted control : SM86Control .
    sm86EncodeInstructionFields
      sm86OpcodeBranch
      guard
      control
      (sm86InstructionPrependWord32Field
        sm86InstructionNaturalThirtyTwo
        offset
        (sm86InstructionOneWord32Field sm86InstructionNaturalSixtyFour descriptor)))

-- ---- system forms (AlphaOS) ----
-- Every word below is the one ptxas 12.9 writes for sm_86
-- (experiments/sm86-opcodes/sources/ptxas-oracle.tsv; Proof.SM86SystemEncoding
-- checks them), with one normalization: the .E memory forms name the uniform
-- register holding the global-memory descriptor, and Alpha keeps it in UR4 as
-- its LDG/STG/RED encoders above already do (ptxas happened to pick UR6).
-- The descriptor word's other fields: bits 77..80 memory order (strong.CTA 5,
-- strong.GPU 7, strong.SYS 10), bits 87..90 the atomic operation (Mesa NAK
-- AtomOp: add 0, min 1, max 2, inc 3, dec 4, and 5, or 6, xor 7, exch 8).
def sm86MemoryScopeOrderNatural =
  (lambda unrestricted scope : SM86MemoryScope .
    eliminate
      SM86MemoryScope
      (lambda unrestricted current : SM86MemoryScope . Nat)
      scope
      (branch SM86ScopeBlock . 5)
      (branch SM86ScopeDevice . 7)
      (branch SM86ScopeSystem . 10))

def sm86MemoryScopeBarrierNatural =
  (lambda unrestricted scope : SM86MemoryScope .
    eliminate
      SM86MemoryScope
      (lambda unrestricted current : SM86MemoryScope . Nat)
      scope
      (branch SM86ScopeBlock . 0)
      (branch SM86ScopeDevice . 2)
      (branch SM86ScopeSystem . 3))

def sm86AtomicOperationNatural =
  (lambda unrestricted operation : SM86AtomicOperation .
    eliminate
      SM86AtomicOperation
      (lambda unrestricted current : SM86AtomicOperation . Nat)
      operation
      (branch SM86AtomicAdd . 0)
      (branch SM86AtomicMinimum . 1)
      (branch SM86AtomicMaximum . 2)
      (branch SM86AtomicIncrement . 3)
      (branch SM86AtomicDecrement . 4)
      (branch SM86AtomicAnd . 5)
      (branch SM86AtomicOr . 6)
      (branch SM86AtomicXor . 7)
      (branch SM86AtomicExchange . 8))

-- 1 for the one operation RED has no form for
def sm86AtomicOperationIsExchange =
  (lambda unrestricted operation : SM86AtomicOperation .
    nat-less-than 7 (sm86AtomicOperationNatural operation))

def sm86IntegerComparisonNatural =
  (lambda unrestricted comparison : SM86IntegerComparison .
    eliminate
      SM86IntegerComparison
      (lambda unrestricted current : SM86IntegerComparison . Nat)
      comparison
      (branch SM86CompareLess . 1)
      (branch SM86CompareEqual . 2)
      (branch SM86CompareLessOrEqual . 3)
      (branch SM86CompareGreater . 4)
      (branch SM86CompareNotEqual . 5)
      (branch SM86CompareGreaterOrEqual . 6))

def sm86IntegerSignednessNatural =
  (lambda unrestricted signedness : SM86IntegerSignedness .
    eliminate
      SM86IntegerSignedness
      (lambda unrestricted current : SM86IntegerSignedness . Nat)
      signedness
      (branch SM86Unsigned . 0)
      (branch SM86Signed . 1))

def sm86PredicateSenseNatural =
  (lambda unrestricted sense : SM86PredicateSense .
    eliminate
      SM86PredicateSense
      (lambda unrestricted current : SM86PredicateSense . Nat)
      sense
      (branch SM86PredicateAsIs . 0)
      (branch SM86PredicateInverted . 1))

def sm86FenceModeNatural =
  (lambda unrestricted mode : SM86FenceMode .
    eliminate
      SM86FenceMode
      (lambda unrestricted current : SM86FenceMode . Nat)
      mode
      (branch SM86FenceSequential . 0)
      (branch SM86FenceAcquireRelease . 1))

def sm86IntegerExtremumNatural =
  (lambda unrestricted mode : SM86FloatExtremum .
    eliminate
      SM86FloatExtremum
      (lambda unrestricted current : SM86FloatExtremum . Nat)
      mode
      (branch SM86FloatMinimum . 0)
      (branch SM86FloatMaximum . 1))

def sm86PredicateNatural =
  (lambda unrestricted predicate : SM86Predicate . byte-to-nat (sm86PredicateNumber predicate))

-- BRX, CALL.REL and RET.REL carry a signed 32-bit offset at 32..63 whose sign
-- also fills bits 64..81 (0x3ffff in the descriptor word when negative).
def sm86RelativeTransferWord =
  (lambda unrestricted base : Nat . unrestricted offset : SM86Unsigned32 .
    then (nat-add base)
      (nat-multiply 262143)
      (nat-less-than 2147483647)
      sm86Unsigned32Natural
      offset)

def sm86EncodeRelativeTransfer =
  (lambda unrestricted opcode : Nat .
    unrestricted base : Nat .
    unrestricted guard : SM86InstructionGuard .
    unrestricted register : SM86Register .
    unrestricted offset : SM86Unsigned32 .
    unrestricted control : SM86Control .
    then (sm86EncodeInstructionFields opcode guard control)
      (sm86InstructionPrependField 24 8 (sm86RegisterNatural register))
      (sm86InstructionPrependWord32Field 32 offset)
      (sm86InstructionOneWord32Field 64)
      sm86Unsigned32FromNaturalTruncated
      (sm86RelativeTransferWord base)
      offset)

-- Opcodes (base | form << 9): ATOMG 0x9a8, ATOMG.CAS 0x3a9, RED 0x98e,
-- MEMBAR 0x992, ERRBAR 0x9ab, CCTL 0x98f, NANOSLEEP 0x95d, ISETP 0x20c,
-- SEL 0x207, IMNMX 0x217, IMAD 0x224, BRX 0x949, CALL.REL 0x344,
-- RET.REL 0x950, POPC 0x309, VOTE 0x806, BAR 0xb1d.
def sm86EncodeSystem =
  (lambda unrestricted guard : SM86InstructionGuard .
    unrestricted operation : SM86SystemOperation .
    unrestricted control : SM86Control .
    eliminate
      SM86SystemOperation
      (lambda unrestricted current : SM86SystemOperation . SM86InstructionEncodingResult)
      operation
      (branch
        SM86AtomicGlobal
        atomic
        scope
        destination
        address
        value
        offset
        .
        then (sm86EncodeInstructionFields 2472 guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
          (sm86InstructionPrependField 24 8 (sm86RegisterNatural address))
          (sm86InstructionPrependField 32 8 (sm86RegisterNatural value))
          (sm86InstructionPrependField 40 sm86GlobalOffsetFieldBits (sm86Unsigned32Natural offset))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nest nat-add
            136184260
            (nat-multiply 8388608 (sm86AtomicOperationNatural atomic)))
            (nat-multiply 8192) (sm86MemoryScopeOrderNatural scope))
      (branch
        SM86AtomicCompareSwapGlobal
        scope
        destination
        address
        compare
        value
        offset
        .
        then (sm86EncodeInstructionFields 937 guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
          (sm86InstructionPrependField 24 8 (sm86RegisterNatural address))
          (sm86InstructionPrependField 32 8 (sm86RegisterNatural compare))
          (sm86InstructionPrependField 40 sm86GlobalOffsetFieldBits (sm86Unsigned32Natural offset))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nest nat-add
            1966336
            (sm86RegisterNatural value))
            (nat-multiply 8192) (sm86MemoryScopeOrderNatural scope))
      (branch
        SM86ReduceGlobal
        reduction
        scope
        address
        value
        offset
        .
        nat-eliminate
          (lambda unrestricted current : Nat . SM86InstructionEncodingResult)
          (then (sm86EncodeInstructionFields 2446 guard control)
            (sm86InstructionPrependField 24 8 (sm86RegisterNatural address))
            (sm86InstructionPrependField 32 8 (sm86RegisterNatural value))
            (sm86InstructionPrependField
              40
              sm86GlobalOffsetFieldBits
              (sm86Unsigned32Natural offset))
            (sm86InstructionOneWord32Field 64)
            sm86Unsigned32FromNaturalTruncated
            (nest nat-add
              202375556
              (nat-multiply 8388608 (sm86AtomicOperationNatural reduction)))
              (nat-multiply 8192) (sm86MemoryScopeOrderNatural scope))
          (lambda unrestricted predecessor : Nat .
            unrestricted induction : SM86InstructionEncodingResult .
            sm86InstructionUnsupported 2446)
          (sm86AtomicOperationIsExchange reduction))
      (branch
        SM86MemoryBarrier
        scope
        mode
        .
        then (sm86EncodeInstructionFields 2450 guard control)
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nat-add (nat-multiply 4096 (sm86MemoryScopeBarrierNatural scope)))
          (nat-multiply 32768)
          sm86FenceModeNatural
          mode)
      (branch
        SM86ErrorBarrier
        .
        sm86EncodeInstructionFields 2475 guard control sm86InstructionFieldsEmpty)
      (branch
        SM86InvalidateDataCache
        .
        then (sm86EncodeInstructionFields 2447 guard control)
          (sm86InstructionPrependField 24 8 255)
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          33554432)
      (branch
        SM86LoadGlobalStrong
        scope
        destination
        address
        offset
        .
        then (sm86EncodeInstructionFields sm86OpcodeLoadGlobal guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
          (sm86InstructionPrependField 24 8 (sm86RegisterNatural address))
          (sm86InstructionPrependField 32 8 4)
          (sm86InstructionPrependField 40 sm86GlobalOffsetFieldBits (sm86Unsigned32Natural offset))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nat-add 203299072)
          (nat-multiply 8192)
          sm86MemoryScopeOrderNatural
          scope)
      (branch
        SM86StoreGlobalStrong
        scope
        address
        value
        offset
        .
        then (sm86EncodeInstructionFields sm86OpcodeStoreGlobal guard control)
          (sm86InstructionPrependField 24 8 (sm86RegisterNatural address))
          (sm86InstructionPrependField 32 8 (sm86RegisterNatural value))
          (sm86InstructionPrependField 40 sm86GlobalOffsetFieldBits (sm86Unsigned32Natural offset))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nat-add 202381572)
          (nat-multiply 8192)
          sm86MemoryScopeOrderNatural
          scope)
      (branch
        SM86Sleep
        nanoseconds
        .
        then (sm86EncodeInstructionFields 2397 guard control)
          (sm86InstructionPrependWord32Field 32 nanoseconds)
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          58720256)
      (branch
        SM86CompareRegisters
        comparison
        signedness
        destination
        left
        right
        .
        then (sm86EncodeInstructionFields 524 guard control)
          (sm86InstructionPrependField 24 8 (sm86RegisterNatural left))
          (sm86InstructionPrependField 32 8 (sm86RegisterNatural right))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nest nat-add
            66060400
            (nat-multiply 4096 (sm86IntegerComparisonNatural comparison))
            (nat-multiply 512 (sm86IntegerSignednessNatural signedness)))
            (nat-multiply 131072) (sm86PredicateNatural destination))
      (branch
        SM86SelectRegisters
        destination
        whenTrue
        whenFalse
        predicate
        sense
        .
        then (sm86EncodeInstructionFields 519 guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
          (sm86InstructionPrependField 24 8 (sm86RegisterNatural whenTrue))
          (sm86InstructionPrependField 32 8 (sm86RegisterNatural whenFalse))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nat-add (nat-multiply 8388608 (sm86PredicateNatural predicate)))
          (nat-multiply 67108864)
          sm86PredicateSenseNatural
          sense)
      (branch
        SM86IntegerMinimumOrMaximum
        mode
        signedness
        destination
        left
        right
        .
        then (sm86EncodeInstructionFields 535 guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
          (sm86InstructionPrependField 24 8 (sm86RegisterNatural left))
          (sm86InstructionPrependField 32 8 (sm86RegisterNatural right))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nest nat-add
            58720256
            (nat-multiply 67108864 (sm86IntegerExtremumNatural mode)))
            (nat-multiply 512) (sm86IntegerSignednessNatural signedness))
      (branch
        SM86MoveRegister
        destination
        source
        .
        then (sm86EncodeInstructionFields 548 guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
          (sm86InstructionPrependField 24 8 255)
          (sm86InstructionPrependField 32 8 255)
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nat-add 126746624)
          sm86RegisterNatural
          source)
      (branch
        SM86IntegerMultiplyAddRegister
        destination
        left
        right
        addend
        .
        then (sm86EncodeInstructionFields 548 guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
          (sm86InstructionPrependField 24 8 (sm86RegisterNatural left))
          (sm86InstructionPrependField 32 8 (sm86RegisterNatural right))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nat-add 126746624)
          sm86RegisterNatural
          addend)
      -- SR_TID.* and SR_CTAID.* (0x20 .. 0x27) are refused: the grid
      -- rewrites (GroupedLaunch, GridBand) find grid coordinates only in
      -- SM86SpecialToRegister, so they must not arrive another way.
      (branch
        SM86ReadSystemRegister
        destination
        code
        .
        nat-eliminate
          (lambda unrestricted current : Nat . SM86InstructionEncodingResult)
          (then (sm86EncodeInstructionFields sm86OpcodeSpecialToRegister guard control)
            (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
            (sm86InstructionOneField 72 8)
            byte-to-nat
            code)
          (lambda unrestricted predecessor : Nat .
            unrestricted induction : SM86InstructionEncodingResult .
            sm86InstructionUnsupported 2329)
          (naturalAnd (nat-less-than 31 (byte-to-nat code)) (nat-less-than (byte-to-nat code) 40)))
      (branch
        SM86BranchIndirect
        register
        offset
        .
        sm86EncodeRelativeTransfer 2377 58720256 guard register offset control)
      (branch
        SM86CallIndirect
        register
        offset
        .
        sm86EncodeRelativeTransfer 836 62914560 guard register offset control)
      (branch
        SM86ReturnIndirect
        register
        offset
        .
        sm86EncodeRelativeTransfer 2384 62914560 guard register offset control)
      (branch
        SM86PopulationCount
        destination
        source
        .
        then (sm86EncodeInstructionFields 777 guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
          (sm86InstructionOneField 32 8)
          sm86RegisterNatural
          source)
      (branch
        SM86VoteBallot
        destination
        predicate
        sense
        .
        then (sm86EncodeInstructionFields 2054 guard control)
          (sm86InstructionPrependField 16 8 (sm86RegisterNatural destination))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          (nest nat-add
            917760
            (nat-multiply 8388608 (sm86PredicateNatural predicate)))
            (nat-multiply 67108864) (sm86PredicateSenseNatural sense))
      (branch
        SM86BarrierArrive
        barrier
        threads
        .
        then (sm86EncodeInstructionFields 2845 guard control)
          (sm86InstructionPrependField 42 12 threads)
          (sm86InstructionPrependField 54 4 (byte-to-nat barrier))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          8192)
      -- BSSY 0x945 / BSYNC 0x941: the barrier at bit 16, BSSY's
      -- displacement at 32, 0x03800000 above (ptxas's words; sm_121's add
      -- .RECONVERGENT at bit 73)
      (branch
        SM86ConvergenceStart
        barrier
        displacement
        .
        then (sm86EncodeInstructionFields 2373 guard control)
          (sm86InstructionPrependField 16 4 (byte-to-nat barrier))
          (sm86InstructionPrependField 32 32 (sm86Unsigned32Natural displacement))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          58720256)
      (branch
        SM86ConvergenceWait
        barrier
        .
        then (sm86EncodeInstructionFields 2369 guard control)
          (sm86InstructionPrependField 16 4 (byte-to-nat barrier))
          (sm86InstructionOneWord32Field 64)
          sm86Unsigned32FromNaturalTruncated
          58720256)
      (branch
        SM86LabelDistance
        destination
        from
        to
        .
        sm86EncodeMoveImmediate
          guard
          destination
          (pipe to
            sm86Unsigned32Natural
            (nat-add 4294967296)
            (nat-subtract (sm86Unsigned32Natural from))
            (nat-modulo 4294967296)
            sm86Unsigned32FromNaturalTruncated)
          control))

def sm86EncodeInstruction =
  (lambda unrestricted instruction : SM86Instruction .
    eliminate
      SM86Instruction
      (lambda unrestricted current : SM86Instruction . SM86InstructionEncodingResult)
      instruction
      (branch
        SM86InstructionValue
        guard
        body
        .
        eliminate
          SM86InstructionBody
          (lambda unrestricted current : SM86InstructionBody . SM86InstructionEncodingResult)
          body
          (branch
            SM86MoveConstant
            destination
            bank
            offset
            control
            .
            sm86EncodeMoveConstant guard destination bank offset control)
          (branch
            SM86SpecialToRegister
            destination
            special
            control
            .
            sm86EncodeSpecialToRegister guard destination special control)
          (branch
            SM86MoveImmediate
            destination
            immediate
            control
            .
            sm86EncodeMoveImmediate guard destination immediate control)
          (branch
            SM86IntegerMultiplyAddConstant
            destination
            left
            bank
            offset
            addend
            control
            .
            sm86EncodeIntegerMultiplyAddConstant guard destination left bank offset addend control)
          (branch
            SM86IntegerMultiplyAddImmediate
            destination
            left
            immediate
            addend
            control
            .
            sm86EncodeIntegerMultiplyAddImmediate guard destination left immediate addend control)
          (branch
            SM86IntegerMultiplyAddWideConstant
            destination
            left
            right
            bank
            offset
            control
            .
            sm86EncodeIntegerMultiplyAddWideConstant
              guard
              destination
              left
              right
              bank
              offset
              control)
          (branch
            SM86IntegerAddThreeImmediate
            destination
            left
            immediate
            control
            .
            sm86EncodeIntegerAdd3Immediate guard destination left immediate control)
          (branch
            SM86IntegerAddThreeRegister
            destination
            left
            right
            control
            .
            sm86EncodeIntegerAdd3Register guard destination left right control)
          (branch
            SM86ShiftRightImmediate
            destination
            source
            amount
            control
            .
            sm86EncodeShiftRightImmediate guard destination source amount control)
          (branch
            SM86LogicThreeInputTruthTable
            destination
            left
            right
            truthTable
            control
            .
            sm86EncodeLogic3 guard destination left right truthTable control)
          (branch
            SM86IntegerToFloat
            destination
            source
            control
            .
            sm86EncodeIntegerToFloat guard destination source control)
          (branch
            SM86FloatAdd
            destination
            left
            right
            control
            .
            sm86EncodeFloatAdd guard destination left right control)
          (branch
            SM86FloatMultiply
            destination
            left
            right
            control
            .
            sm86EncodeFloatMultiply guard destination left right control)
          (branch
            SM86Float64Binary
            operation
            destination
            left
            right
            control
            .
            sm86EncodeFloat64Binary guard operation destination left right control)
          (branch
            SM86FloatConvert
            conversion
            control
            .
            sm86EncodeFloatConvert guard conversion control)
          (branch
            SM86FloatFusedMultiplyAdd
            destination
            left
            right
            addend
            control
            .
            sm86EncodeFloatFusedMultiplyAdd guard destination left right addend control)
          (branch
            SM86MultiFunctionUnitApproximation
            destination
            source
            operation
            control
            .
            sm86EncodeMultiFunction guard destination source operation control)
          (branch
            SM86FloatMinimumOrMaximum
            destination
            left
            right
            extremum
            control
            .
            sm86EncodeFloatExtremum guard destination left right extremum control)
          (branch
            SM86FloatNegate
            destination
            source
            control
            .
            sm86EncodeFloatNegate guard destination source control)
          (branch
            SM86FloatPairToPackedHalfPair
            destination
            sourceHigh
            sourceLow
            control
            .
            sm86EncodeFloat32PairToHalf2 guard destination sourceHigh sourceLow control)
          (branch
            SM86FloatPairToPackedBFloat16Pair
            destination
            sourceHigh
            sourceLow
            control
            .
            sm86EncodeFloat32PairToBFloat16Pair guard destination sourceHigh sourceLow control)
          (branch
            SM86FloatPairToPackedFloat8Pair
            destination
            sourceHigh
            sourceLow
            merge
            mergeHalf
            format
            control
            .
            sm86EncodeFloat32PairToFloat8Pair
              guard
              destination
              sourceHigh
              sourceLow
              merge
              mergeHalf
              format
              control)
          (branch
            SM86FloatArithmetic
            operation
            destination
            left
            right
            addend
            modifiers
            control
            .
            sm86EncodeFloatArithmetic
              guard
              operation
              destination
              left
              right
              addend
              modifiers
              control)
          (branch
            SM86HalfToFloat
            destination
            source
            selector
            control
            .
            sm86EncodeHalfToFloat guard destination source selector control)
          (branch
            SM86BFloat16ToFloat
            destination
            source
            selector
            control
            .
            sm86EncodeBFloat16ToFloat guard destination source selector control)
          (branch
            SM86TensorCoreHalfMatrixMultiplyAccumulate16x8x16Float32
            destination
            fragmentA
            fragmentB
            accumulator
            control
            .
            sm86EncodeHMMA16816F32F16 guard destination fragmentA fragmentB accumulator control)
          (branch
            SM86TensorCoreBFloat16MatrixMultiplyAccumulate16x8x16Float32
            destination
            fragmentA
            fragmentB
            accumulator
            control
            .
            sm86EncodeHMMA16816F32BF16 guard destination fragmentA fragmentB accumulator control)
          (branch
            SM86TensorCoreHalfMatrixMultiplyAccumulate16x8x16Half
            destination
            fragmentA
            fragmentB
            accumulator
            control
            .
            sm86EncodeHMMA16816F16F16 guard destination fragmentA fragmentB accumulator control)
          (branch
            SM86TensorCoreInteger8MatrixMultiplyAccumulate16x8x32Integer32
            destination
            fragmentA
            fragmentB
            accumulator
            control
            .
            sm86EncodeIMMA16832S8S8 guard destination fragmentA fragmentB accumulator control)
          (branch
            SM86LoadGlobal
            destination
            address
            offset
            control
            .
            sm86EncodeLoadGlobal guard destination address offset control)
          (branch
            SM86LoadGlobalWide
            destination
            address
            offset
            control
            .
            sm86EncodeLoadGlobalWide guard destination address offset control)
          (branch
            SM86WarpShuffle
            destination
            source
            lane
            segment
            mode
            control
            .
            sm86EncodeShuffle guard destination source lane segment mode control)
          (branch
            SM86LoadShared
            destination
            address
            offset
            control
            .
            sm86EncodeLoadShared guard destination address offset control)
          (branch
            SM86LoadSharedMatrix
            destination
            address
            offset
            count
            transpose
            control
            .
            sm86EncodeLoadSharedMatrix guard destination address offset count transpose control)
          (branch
            SM86StoreShared
            address
            value
            offset
            control
            .
            sm86EncodeStoreShared guard address value offset control)
          (branch
            SM86LoadGlobalToShared
            address
            offset
            source
            sourceOffset
            control
            .
            sm86EncodeLoadGlobalToShared guard address offset source sourceOffset control)
          (branch SM86CommitAsyncGroup control . sm86EncodeCommitAsyncGroup guard control)
          (branch
            SM86WaitAsyncGroups
            count
            control
            .
            sm86EncodeWaitAsyncGroups guard count control)
          (branch SM86BarrierSynchronize control . sm86EncodeBarrierSynchronize guard control)
          (branch
            SM86BarrierSynchronizeCount
            barrier
            threads
            control
            .
            sm86EncodeBarrierSynchronizeCount guard barrier threads control)
          (branch
            SM86StoreGlobal
            address
            value
            offset
            control
            .
            sm86EncodeStoreGlobal guard address value offset control)
          (branch
            SM86StoreGlobalWide
            address
            value
            offset
            control
            .
            sm86EncodeStoreGlobalWide guard address value offset control)
          (branch
            SM86StoreGlobal64
            address
            value
            offset
            control
            .
            sm86EncodeStoreGlobal64 guard address value offset control)
          (branch
            SM86ReduceGlobalAddFloat32
            address
            value
            offset
            control
            .
            sm86EncodeReduceGlobalAddFloat32 guard address value offset control)
          (branch
            SM86PredicateGreaterThanImmediate
            predicate
            source
            immediate
            control
            .
            sm86EncodePredicateGreater guard predicate source immediate control)
          (branch
            SM86Branch
            offset
            descriptor
            control
            .
            sm86EncodeBranch guard offset descriptor control)
          (branch SM86Exit control . sm86EncodeExit guard control)
          (branch SM86System operation control . sm86EncodeSystem guard operation control)))

def sm86ProgramTelemetryZero =
  (constructor SM86ProgramEncodingTelemetry SM86ProgramEncodingTelemetryValue 0 0 0 0 0)

def sm86ProgramTelemetryInstruction =
  (lambda unrestricted fieldTelemetry : SM86FieldEncodingTelemetry .
    eliminate
      SM86FieldEncodingTelemetry
      (lambda unrestricted current : SM86FieldEncodingTelemetry . SM86ProgramEncodingTelemetry)
      fieldTelemetry
      (branch
        SM86FieldEncodingTelemetryValue
        fields
        bits
        outputBytes
        highest
        .
        constructor
          SM86ProgramEncodingTelemetry
          SM86ProgramEncodingTelemetryValue
          1
          outputBytes
          fields
          bits
          highest))

def sm86MergeProgramTelemetry =
  (lambda unrestricted left : SM86ProgramEncodingTelemetry .
    unrestricted right : SM86ProgramEncodingTelemetry .
    eliminate
      SM86ProgramEncodingTelemetry
      (lambda unrestricted current : SM86ProgramEncodingTelemetry . SM86ProgramEncodingTelemetry)
      left
      (branch
        SM86ProgramEncodingTelemetryValue
        leftInstructions
        leftBytes
        leftFields
        leftBits
        leftHighest
        .
        eliminate
          SM86ProgramEncodingTelemetry
          (lambda unrestricted current : SM86ProgramEncodingTelemetry .
            SM86ProgramEncodingTelemetry)
          right
          (branch
            SM86ProgramEncodingTelemetryValue
            rightInstructions
            rightBytes
            rightFields
            rightBits
            rightHighest
            .
            constructor
              SM86ProgramEncodingTelemetry
              SM86ProgramEncodingTelemetryValue
              (naturalAdd leftInstructions rightInstructions)
              (naturalAdd leftBytes rightBytes)
              (naturalAdd leftFields rightFields)
              (naturalAdd leftBits rightBits)
              (sm86NaturalMaximum leftHighest rightHighest))))

def sm86EncodeProgramFromIndex =
  (lambda unrestricted program : SM86Program .
    eliminate
      SM86Program
      (lambda unrestricted current : SM86Program .
        pi unrestricted instructionIndex : Nat . SM86ProgramEncodingResult)
      program
      (branch
        SM86ProgramEnd
        .
        lambda unrestricted instructionIndex : Nat .
          constructor
            SM86ProgramEncodingResult
            SM86ProgramEncodingSucceeded
            b""
            sm86ProgramTelemetryZero)
      (branch
        SM86ProgramNext
        head
        tail
        ih_tail
        .
        lambda unrestricted instructionIndex : Nat .
          eliminate
            SM86InstructionEncodingResult
            (lambda unrestricted current : SM86InstructionEncodingResult .
              SM86ProgramEncodingResult)
            (sm86EncodeInstruction head)
            (branch
              SM86InstructionEncodingSucceeded
              headBytes
              headTelemetry
              .
              eliminate
                SM86ProgramEncodingResult
                (lambda unrestricted current : SM86ProgramEncodingResult .
                  SM86ProgramEncodingResult)
                (ih_tail (succ instructionIndex))
                (branch
                  SM86ProgramEncodingSucceeded
                  tailBytes
                  tailTelemetry
                  .
                  constructor
                    SM86ProgramEncodingResult
                    SM86ProgramEncodingSucceeded
                    (bytes-append headBytes tailBytes)
                    (sm86MergeProgramTelemetry
                      (sm86ProgramTelemetryInstruction headTelemetry)
                      tailTelemetry))
                (branch
                  SM86ProgramEncodingFailed
                  failureIndex
                  failure
                  failureTelemetry
                  .
                  constructor
                    SM86ProgramEncodingResult
                    SM86ProgramEncodingFailed
                    failureIndex
                    failure
                    (sm86MergeProgramTelemetry
                      (sm86ProgramTelemetryInstruction headTelemetry)
                      failureTelemetry)))
            (branch
              SM86InstructionEncodingFieldFailed
              error
              position
              width
              detail
              fieldTelemetry
              .
              constructor
                SM86ProgramEncodingResult
                SM86ProgramEncodingFailed
                instructionIndex
                (constructor
                  SM86InstructionEncodingResult
                  SM86InstructionEncodingFieldFailed
                  error
                  position
                  width
                  detail
                  fieldTelemetry)
                (sm86ProgramTelemetryInstruction fieldTelemetry))
            (branch
              SM86InstructionEncodingUnsupported
              ordinal
              .
              constructor
                SM86ProgramEncodingResult
                SM86ProgramEncodingFailed
                instructionIndex
                (constructor
                  SM86InstructionEncodingResult
                  SM86InstructionEncodingUnsupported
                  ordinal)
                sm86ProgramTelemetryZero)))

def sm86EncodeProgram =
  (lambda unrestricted program : SM86Program .
    constructor
      SM86ProgramEncodingResult
      SM86ProgramEncodingSucceeded
      (compiler-native-encode SM86Program program)
      sm86ProgramTelemetryZero)

-- Encode structural repetition without first expanding the repeated program.
-- This is the physical-program analogue of a bytes builder: the segment is
-- validated and encoded once, while its already-checked image is retained as
-- a shared chunk until the final build.  Large unrolled schedules therefore
-- remain linear in their output size instead of repeatedly normalizing the
-- same instruction terms.
def sm86RepeatedProgramBytesBuilder =
  (lambda unrestricted payload : Bytes . unrestricted count : Nat .
    nat-eliminate
      (lambda unrestricted current : Nat . BytesBuilder)
      (bytes-builder-empty)
      (lambda unrestricted predecessor : Nat . unrestricted induction : BytesBuilder .
        bytes-builder-append (bytes-builder-chunk payload) induction)
      count)

def sm86ScaleProgramTelemetry =
  (lambda unrestricted telemetry : SM86ProgramEncodingTelemetry . unrestricted count : Nat .
    eliminate
      SM86ProgramEncodingTelemetry
      (lambda unrestricted current : SM86ProgramEncodingTelemetry . SM86ProgramEncodingTelemetry)
      telemetry
      (branch
        SM86ProgramEncodingTelemetryValue
        instructions
        outputBytes
        fields
        bits
        highest
        .
        constructor
          SM86ProgramEncodingTelemetry
          SM86ProgramEncodingTelemetryValue
          (naturalMultiply instructions count)
          (naturalMultiply outputBytes count)
          (naturalMultiply fields count)
          (naturalMultiply bits count)
          highest))

def sm86ProgramEncodingAppend =
  (lambda unrestricted left : SM86ProgramEncodingResult .
    unrestricted right : SM86ProgramEncodingResult .
    eliminate
      SM86ProgramEncodingResult
      (lambda unrestricted current : SM86ProgramEncodingResult . SM86ProgramEncodingResult)
      left
      (branch
        SM86ProgramEncodingSucceeded
        leftBytes
        leftTelemetry
        .
        eliminate
          SM86ProgramEncodingResult
          (lambda unrestricted current : SM86ProgramEncodingResult . SM86ProgramEncodingResult)
          right
          (branch
            SM86ProgramEncodingSucceeded
            rightBytes
            rightTelemetry
            .
            constructor
              SM86ProgramEncodingResult
              SM86ProgramEncodingSucceeded
              (pipe leftBytes
                bytes-builder-chunk
                (bytes-builder-append (bytes-builder-chunk rightBytes))
                bytes-builder-build)
              (sm86MergeProgramTelemetry leftTelemetry rightTelemetry))
          (branch
            SM86ProgramEncodingFailed
            rightFailureIndex
            rightFailure
            rightFailureTelemetry
            .
            eliminate
              SM86ProgramEncodingTelemetry
              (lambda unrestricted current : SM86ProgramEncodingTelemetry .
                SM86ProgramEncodingResult)
              leftTelemetry
              (branch
                SM86ProgramEncodingTelemetryValue
                leftInstructions
                leftOutputBytes
                leftFields
                leftBits
                leftHighest
                .
                constructor
                  SM86ProgramEncodingResult
                  SM86ProgramEncodingFailed
                  (naturalAdd leftInstructions rightFailureIndex)
                  rightFailure
                  (sm86MergeProgramTelemetry leftTelemetry rightFailureTelemetry))))
      (branch
        SM86ProgramEncodingFailed
        leftFailureIndex
        leftFailure
        leftFailureTelemetry
        .
        constructor
          SM86ProgramEncodingResult
          SM86ProgramEncodingFailed
          leftFailureIndex
          leftFailure
          leftFailureTelemetry))

def sm86EncodeProgramRepeated =
  (lambda unrestricted segment : SM86Program . unrestricted count : Nat .
    nat-eliminate
      (lambda unrestricted nonzero : Nat . SM86ProgramEncodingResult)
      (constructor
        SM86ProgramEncodingResult
        SM86ProgramEncodingSucceeded
        b""
        sm86ProgramTelemetryZero)
      (lambda unrestricted predecessor : Nat . unrestricted induction : SM86ProgramEncodingResult .
        eliminate
          SM86ProgramEncodingResult
          (lambda unrestricted current : SM86ProgramEncodingResult . SM86ProgramEncodingResult)
          (sm86EncodeProgram segment)
          (branch
            SM86ProgramEncodingSucceeded
            segmentBytes
            segmentTelemetry
            .
            constructor
              SM86ProgramEncodingResult
              SM86ProgramEncodingSucceeded
              (bytes-builder-build (sm86RepeatedProgramBytesBuilder segmentBytes count))
              (sm86ScaleProgramTelemetry segmentTelemetry count))
          (branch
            SM86ProgramEncodingFailed
            failureIndex
            failure
            failureTelemetry
            .
            constructor
              SM86ProgramEncodingResult
              SM86ProgramEncodingFailed
              failureIndex
              failure
              failureTelemetry))
      (naturalNonzero count))

def sm86InstructionEncodingStableCode =
  (lambda unrestricted result : SM86InstructionEncodingResult .
    eliminate
      SM86InstructionEncodingResult
      (lambda unrestricted current : SM86InstructionEncodingResult . Bytes)
      result
      (branch SM86InstructionEncodingSucceeded encoded telemetry . b"ALPHA-SM86-INS-000")
      (branch
        SM86InstructionEncodingFieldFailed
        error
        position
        width
        detail
        telemetry
        .
        sm86EncodingErrorStableCode error)
      (branch SM86InstructionEncodingUnsupported ordinal . b"ALPHA-SM86-INS-001"))

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.