[llvm-branch-commits] [llvm] [Bitcode] Add ByteCast encoding and decoding (PR #221885)
Anshil Gandhi via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Sep 7 23:05:39 PDT 2026
https://github.com/gandhi56 updated https://github.com/llvm/llvm-project/pull/221885
>From a5cbaee40f93f7b3bd4133a02f3fac31aa1c65e4 Mon Sep 17 00:00:00 2001
From: Anshil Gandhi <Anshil.Gandhi at amd.com>
Date: Mon, 7 Sep 2026 07:45:48 -0500
Subject: [PATCH] [Bitcode] Add ByteCast encoding and decoding
Add CAST_BYTECAST to the bitcode cast opcode table and wire it through the
reader and writer so bytecast instructions round-trip in bitcode.
Co-authored-by: Cursor <cursoragent at cursor.com>
---
llvm/include/llvm/Bitcode/LLVMBitCodes.h | 1 +
llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 1 +
llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 1 +
llvm/test/Bitcode/bytecast.ll | 72 +++++++++++++++++++++++
4 files changed, 75 insertions(+)
create mode 100644 llvm/test/Bitcode/bytecast.ll
diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index 4099b57f5482e..1422912900d2e 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -471,6 +471,7 @@ enum CastOpcodes {
CAST_BITCAST = 11,
CAST_ADDRSPACECAST = 12,
CAST_PTRTOADDR = 13,
+ CAST_BYTECAST = 14,
};
/// UnaryOpcodes - These are values used in the bitcode files to encode which
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 35e71c93c0109..888dbd8f8929b 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1339,6 +1339,7 @@ static int getDecodedCastOpcode(unsigned Val) {
case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
case bitc::CAST_BITCAST : return Instruction::BitCast;
+ case bitc::CAST_BYTECAST : return Instruction::ByteCast;
case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
}
}
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index e33b6e0050318..acc384fb342e2 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -661,6 +661,7 @@ static unsigned getEncodedCastOpcode(unsigned Opcode) {
case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
case Instruction::BitCast : return bitc::CAST_BITCAST;
+ case Instruction::ByteCast: return bitc::CAST_BYTECAST;
case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
}
}
diff --git a/llvm/test/Bitcode/bytecast.ll b/llvm/test/Bitcode/bytecast.ll
new file mode 100644
index 0000000000000..ef97e289085e7
--- /dev/null
+++ b/llvm/test/Bitcode/bytecast.ll
@@ -0,0 +1,72 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s
+
+ at g = global i64 0
+
+; CHECK: @ce.ptr.to.byte = global b64 bytecast (ptr @g to b64)
+ at ce.ptr.to.byte = global b64 bytecast (ptr @g to b64)
+
+define b64 @int_to_byte(i64 %i) {
+ ; CHECK: %b = bytecast i64 %i to b64
+ %b = bytecast i64 %i to b64
+ ret b64 %b
+}
+
+define i64 @byte_to_int(b64 %b) {
+ ; CHECK: %i = bytecast b64 %b to i64
+ %i = bytecast b64 %b to i64
+ ret i64 %i
+}
+
+define double @byte_to_fp(b64 %b) {
+ ; CHECK: %d = bytecast b64 %b to double
+ %d = bytecast b64 %b to double
+ ret double %d
+}
+
+define b64 @fp_to_byte(double %d) {
+ ; CHECK: %b = bytecast double %d to b64
+ %b = bytecast double %d to b64
+ ret b64 %b
+}
+
+define ptr @byte_to_ptr(b64 %b) {
+ ; CHECK: %p = bytecast b64 %b to ptr
+ %p = bytecast b64 %b to ptr
+ ret ptr %p
+}
+
+define b64 @ptr_to_byte(ptr %p) {
+ ; CHECK: %b = bytecast ptr %p to b64
+ %b = bytecast ptr %p to b64
+ ret b64 %b
+}
+
+define ptr addrspace(1) @byte_to_ptr_as1(b64 %b) {
+ ; CHECK: %p = bytecast b64 %b to ptr addrspace(1)
+ %p = bytecast b64 %b to ptr addrspace(1)
+ ret ptr addrspace(1) %p
+}
+
+define <4 x i8> @byte_vector_to_int_vector(<4 x b8> %b) {
+ ; CHECK: %i = bytecast <4 x b8> %b to <4 x i8>
+ %i = bytecast <4 x b8> %b to <4 x i8>
+ ret <4 x i8> %i
+}
+
+define i64 @byte_vector_to_scalar(<2 x b32> %b) {
+ ; CHECK: %i = bytecast <2 x b32> %b to i64
+ %i = bytecast <2 x b32> %b to i64
+ ret i64 %i
+}
+
+define <2 x b32> @scalar_to_byte_vector(i64 %i) {
+ ; CHECK: %b = bytecast i64 %i to <2 x b32>
+ %b = bytecast i64 %i to <2 x b32>
+ ret <2 x b32> %b
+}
+
+define b8 @byte_to_byte(b8 %b) {
+ ; CHECK: %b2 = bytecast b8 %b to b8
+ %b2 = bytecast b8 %b to b8
+ ret b8 %b2
+}
More information about the llvm-branch-commits
mailing list