https://github.com/gandhi56 created https://github.com/llvm/llvm-project/pull/221886
## Summary - Add `LLVMBuildByteCast` C API builder mirroring `LLVMBuildBitCast`. - Teach `llvm-c-test --echo` to clone `bytecast` instructions via the new builder. - Add `llvm/test/Bindings/llvm-c/bytecast.ll` round-trip coverage. ## Test plan - [x] `ninja check-llvm` passes on this branch - [x] `llvm/test/Bindings/llvm-c/bytecast.ll` passes Depends on #221884 Made with [Cursor](https://cursor.com) >From f68cf9f6134fe8983d7ed6a8388427967c1f51c2 Mon Sep 17 00:00:00 2001 From: Anshil Gandhi <[email protected]> Date: Mon, 7 Sep 2026 08:10:16 -0500 Subject: [PATCH] [LLVM-C] Add LLVMBuildByteCast Expose a C API builder for the bytecast instruction, mirroring the existing LLVMBuildBitCast entry point, and teach llvm-c-test --echo to clone bytecast so the new entry point is exercised by a round-trip test. Co-authored-by: Cursor <[email protected]> --- llvm/include/llvm-c/Core.h | 2 + llvm/lib/IR/Core.cpp | 6 +++ llvm/test/Bindings/llvm-c/bytecast.ll | 53 +++++++++++++++++++++++++++ llvm/tools/llvm-c-test/echo.cpp | 5 +++ 4 files changed, 66 insertions(+) create mode 100644 llvm/test/Bindings/llvm-c/bytecast.ll diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index f4431536dc481..f7e721cc64197 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -5063,6 +5063,8 @@ LLVM_C_ABI LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef, LLVMValueRef Val, LLVMTypeRef DestTy, const char *Name); LLVM_C_ABI LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef, LLVMValueRef Val, LLVMTypeRef DestTy, const char *Name); +LLVM_C_ABI LLVMValueRef LLVMBuildByteCast(LLVMBuilderRef, LLVMValueRef Val, + LLVMTypeRef DestTy, const char *Name); LLVM_C_ABI LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef, LLVMValueRef Val, LLVMTypeRef DestTy, const char *Name); diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index d1ee8411e392c..a2b9067ddafe8 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -4358,6 +4358,12 @@ LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val, return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name)); } +LLVMValueRef LLVMBuildByteCast(LLVMBuilderRef B, LLVMValueRef Val, + LLVMTypeRef DestTy, const char *Name) { + return wrap(unwrap(B)->CreateCast(Instruction::ByteCast, unwrap(Val), + unwrap(DestTy), Name)); +} + LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val, LLVMTypeRef DestTy, const char *Name) { return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name)); diff --git a/llvm/test/Bindings/llvm-c/bytecast.ll b/llvm/test/Bindings/llvm-c/bytecast.ll new file mode 100644 index 0000000000000..7552722ea94b7 --- /dev/null +++ b/llvm/test/Bindings/llvm-c/bytecast.ll @@ -0,0 +1,53 @@ +; RUN: llvm-as < %s | llvm-dis > %t.orig +; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo +; RUN: diff -w %t.orig %t.echo + +define b64 @int_to_byte(i64 %i) { + %b = bytecast i64 %i to b64 + ret b64 %b +} + +define i64 @byte_to_int(b64 %b) { + %i = bytecast b64 %b to i64 + ret i64 %i +} + +define double @byte_to_fp(b64 %b) { + %d = bytecast b64 %b to double + ret double %d +} + +define b64 @fp_to_byte(double %d) { + %b = bytecast double %d to b64 + ret b64 %b +} + +define ptr @byte_to_ptr(b64 %b) { + %p = bytecast b64 %b to ptr + ret ptr %p +} + +define b64 @ptr_to_byte(ptr %p) { + %b = bytecast ptr %p to b64 + ret b64 %b +} + +define ptr addrspace(1) @byte_to_ptr_as1(b64 %b) { + %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) { + %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) { + %i = bytecast <2 x b32> %b to i64 + ret i64 %i +} + +define <2 x b32> @scalar_to_byte_vector(i64 %i) { + %b = bytecast i64 %i to <2 x b32> + ret <2 x b32> %b +} diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp index 1574d0ffe84cc..3b5c800206d37 100644 --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -814,6 +814,11 @@ struct FunCloner { Dst = LLVMBuildBitCast(Builder, V, CloneType(Src), Name); break; } + case LLVMByteCast: { + LLVMValueRef V = CloneValue(LLVMGetOperand(Src, 0)); + Dst = LLVMBuildByteCast(Builder, V, CloneType(Src), Name); + break; + } case LLVMICmp: { LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src); LLVMBool IsSameSign = LLVMGetICmpSameSign(Src); _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
