[llvm] f83697d - [DirectX] Handle byte type in DXILBitcodeWriter (#187157)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 25 13:57:15 PDT 2026


Author: Justin Bogner
Date: 2026-03-25T20:57:08Z
New Revision: f83697d69f6dd489717a4798a06821ff6c9dd158

URL: https://github.com/llvm/llvm-project/commit/f83697d69f6dd489717a4798a06821ff6c9dd158
DIFF: https://github.com/llvm/llvm-project/commit/f83697d69f6dd489717a4798a06821ff6c9dd158.diff

LOG: [DirectX] Handle byte type in DXILBitcodeWriter (#187157)

The byte type was introduced to LLVM IR in #186888. DXILBitcodeWriter
needs to handle this type by generating valid LLVM 3.7 era bitcode,
which is as simple as treating byte equivalently to an integer.

Added: 
    llvm/test/CodeGen/DirectX/Bitcode/byte.ll
    llvm/test/tools/dxil-dis/byte.ll

Modified: 
    llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
index 92341721c2b1a..9220a8b163aaf 100644
--- a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
+++ b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
@@ -1051,6 +1051,12 @@ void DXILBitcodeWriter::writeTypeTable() {
     case Type::MetadataTyID:
       Code = bitc::TYPE_CODE_METADATA;
       break;
+    case Type::ByteTyID:
+      // BYTE: [width]
+      // Note: we downgrade by converting to the equivalent integer.
+      Code = bitc::TYPE_CODE_INTEGER;
+      TypeVals.push_back(T->getByteBitWidth());
+      break;
     case Type::IntegerTyID:
       // INTEGER: [width]
       Code = bitc::TYPE_CODE_INTEGER;
@@ -2014,6 +2020,22 @@ void DXILBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
         }
         Code = bitc::CST_CODE_WIDE_INTEGER;
       }
+    } else if (const ConstantByte *BV = dyn_cast<ConstantByte>(C)) {
+      // Note: we downgrade by converting to the equivalent integer - this logic
+      // should match the `ConstantInt` case above.
+      if (BV->getBitWidth() <= 64) {
+        uint64_t V = BV->getSExtValue();
+        emitSignedInt64(Record, V);
+        Code = bitc::CST_CODE_INTEGER;
+        AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
+      } else { // Wide bytes, > 64 bits in size.
+        unsigned NWords = BV->getValue().getActiveWords();
+        const uint64_t *RawWords = BV->getValue().getRawData();
+        for (unsigned i = 0; i != NWords; ++i) {
+          emitSignedInt64(Record, RawWords[i]);
+        }
+        Code = bitc::CST_CODE_WIDE_INTEGER;
+      }
     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
       Code = bitc::CST_CODE_FLOAT;
       Type *Ty = CFP->getType()->getScalarType();
@@ -2065,7 +2087,7 @@ void DXILBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
                    dyn_cast<ConstantDataSequential>(C)) {
       Code = bitc::CST_CODE_DATA;
       Type *EltTy = CDS->getElementType();
-      if (isa<IntegerType>(EltTy)) {
+      if (isa<IntegerType>(EltTy) || isa<ByteType>(EltTy)) {
         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
           Record.push_back(CDS->getElementAsInteger(i));
       } else if (EltTy->isFloatTy()) {

diff  --git a/llvm/test/CodeGen/DirectX/Bitcode/byte.ll b/llvm/test/CodeGen/DirectX/Bitcode/byte.ll
new file mode 100644
index 0000000000000..c53670a7577ea
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/Bitcode/byte.ll
@@ -0,0 +1,87 @@
+;; Verify that byte types are encoded as integer types in DXIL bitcode.
+; RUN: llc --filetype=obj %s --stop-after=dxil-write-bitcode -o %t
+; RUN: llvm-bcanalyzer --dump %t | FileCheck %s
+
+target triple = "dxil--shadermodel6.5-library"
+
+;; Since bitcode defines types up front and then refers back to them using
+;; their indices, we unfortunately need to hardcode the entire type block here,
+;; which means this test may be brittle.
+
+;; See https://llvm.org/docs/BitCodeFormat.html for help interpreting below.
+;
+; CHECK:      <TYPE_BLOCK_ID
+; CHECK-NEXT:   <NUMENTRY op0=24/>
+; CHECK-NEXT:   <INTEGER op0=8/>
+; CHECK-NEXT:   <POINTER {{.*}} op0=0 op1=0/>
+;; 2: STRUCT_NAME + OPAQUE only take up one index here.
+; CHECK-NEXT:   <STRUCT_NAME {{.*}}/> record string = 'dxilOpaquePtrReservedName'
+; CHECK-NEXT:   <OPAQUE op0=0/>
+;; 3: i8
+; CHECK-NEXT:   <INTEGER op0=8/>
+;; 4: [32 x i8]
+; CHECK-NEXT:   <ARRAY {{.*}} op0=32 op1=3/>
+;; 5-12: The return and operand types of @bytes, except `i8`,
+; CHECK-NEXT:   <VOID/>
+; CHECK-NEXT:   <INTEGER op0=1/>
+; CHECK-NEXT:   <INTEGER op0=3/>
+; CHECK-NEXT:   <INTEGER op0=5/>
+; CHECK-NEXT:   <INTEGER op0=16/>
+; CHECK-NEXT:   <INTEGER op0=32/>
+; CHECK-NEXT:   <INTEGER op0=64/>
+; CHECK-NEXT:   <INTEGER op0=128/>
+;; 13: <8 x i5>
+; CHECK-NEXT:   <VECTOR op0=8 op1=8/>
+;; 14: <2 x i64>
+; CHECK-NEXT:   <VECTOR op0=2 op1=11/>
+;; 15: void(i1, i3, i5, i8, i16, i32, i64, i128, <8 x i5>, <2 x i64>)
+; CHECK-NEXT:   <FUNCTION {{.*}} op0=0 op1=5 op2=6 op3=7 op4=8 op5=3 op6=9 op7=10 op8=11 op9=12 op10=13 op11=14/>
+; CHECK-NEXT:   <POINTER {{.*}} op0=15 op1=0/>
+;; 17: b32()
+; CHECK-NEXT:   <FUNCTION {{.*}} op0=0 op1=10/>
+; CHECK-NEXT:   <POINTER {{.*}} op0=17 op1=0/>
+;; 19: b128()
+; CHECK-NEXT:   <FUNCTION {{.*}} op0=0 op1=12/>
+; CHECK-NEXT:   <POINTER {{.*}} op0=19 op1=0/>
+; CHECK-NEXT:   <POINTER {{.*}} op0=4 op1=0/>
+; CHECK-NEXT:   <METADATA/>
+; CHECK-NEXT:   <INTEGER op0=32/>
+; CHECK-NEXT: </TYPE_BLOCK_ID>
+
+;; Sanity check that the globals are coherently ordered.
+; CHECK:      <GLOBALVAR {{.*}} op0=4
+; CHECK-NEXT: <FUNCTION op0=15
+; CHECK-NEXT: <FUNCTION op0=17
+; CHECK-NEXT: <FUNCTION op0=19
+
+; CHECK:      <VALUE_SYMTAB
+; CHECK-NEXT:   <ENTRY {{.*}} op0=0 {{.*}}/> record string = 'a'
+; CHECK-NEXT:   <ENTRY {{.*}} op0=1 {{.*}}/> record string = 'bytes'
+; CHECK-NEXT:   <ENTRY {{.*}} op0=3 {{.*}}/> record string = 'constant128'
+; CHECK-NEXT:   <ENTRY {{.*}} op0=2 {{.*}}/> record string = 'constant32'
+; CHECK-NEXT: </VALUE_SYMTAB>
+
+ at a = common global [32 x b8] zeroinitializer, align 1
+
+define void @bytes(b1 %a, b3 %b, b5 %c, b8 %d, b16 %e, b32 %f, b64 %g, b128 %h, <8 x b5> %i, <2 x b64> %j) {
+  ; CHECK: <FUNCTION_BLOCK
+  ret void
+}
+
+define b32 @constant32() {
+  ; CHECK: <FUNCTION_BLOCK
+  ; CHECK:      <CONSTANTS_BLOCK
+  ; CHECK-NEXT:   <SETTYPE {{.*}} op0=10/>
+  ;; The value here is signed variable-length encoded, so the value is doubled
+  ; CHECK-NEXT:   <INTEGER {{.*}} op0=246/>
+  ret b32 123
+}
+
+define b128 @constant128() {
+  ; CHECK: <FUNCTION_BLOCK
+  ; CHECK:      <CONSTANTS_BLOCK
+  ; CHECK-NEXT:   <SETTYPE {{.*}} op0=12/>
+  ;; The value here is signed variable-length encoded, so the value is doubled
+  ; CHECK-NEXT:   <WIDE_INTEGER op0=24682468/>
+  ret b128 12341234
+}

diff  --git a/llvm/test/tools/dxil-dis/byte.ll b/llvm/test/tools/dxil-dis/byte.ll
new file mode 100644
index 0000000000000..8bca5824e2be4
--- /dev/null
+++ b/llvm/test/tools/dxil-dis/byte.ll
@@ -0,0 +1,26 @@
+; RUN: llc --filetype=obj %s -o - | dxil-dis -o - | FileCheck %s
+target triple = "dxil-unknown-shadermodel6.7-library"
+
+; CHECK: common global [32 x i8] zeroinitializer, align 1
+ at a = common global [32 x b8] zeroinitializer, align 1
+
+define void @bytes(b1 %a, b3 %b, b5 %c, b8 %d, b16 %e, b32 %f, b64 %g, b128 %h, <8 x b5> %i, <2 x b64> %j) {
+  ; Check that we generally convert byte types into int types
+  ; CHECK-LABEL: define void @bytes(i1 %a, i3 %b, i5 %c, i8 %d, i16 %e, i32 %f, i64 %g, i128 %h, <8 x i5> %i, <2 x i64> %j
+  ; CHECK-NEXT:    ret void
+  ret void
+}
+
+define b32 @constant32() {
+  ; Check that we write constant byte types as constant ints
+  ; CHECK-LABEL: define i32 @constant32()
+  ; CHECK:         ret i32 255
+  ret b32 255
+}
+
+define b128 @constant128() {
+  ; Check that we write large constant byte types as constant ints
+  ; CHECK-LABEL: define i128 @constant128()
+  ; CHECK:         ret i128 18446744073709551615
+  ret b128 18446744073709551615
+}


        


More information about the llvm-commits mailing list