diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 8ba80209c9a2..a6ff22f3b7de 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -3650,9 +3650,6 @@ mlir::LogicalResult CIRToLLVMSetBitfieldOpLowering::matchAndRewrite( auto resultTy = getTypeConverter()->convertType(op.getType()); - resultVal = createIntCast(rewriter, resultVal, - mlir::cast(resultTy)); - if (info.getIsSigned()) { assert(size <= storageSize); unsigned highBits = storageSize - size; @@ -3663,6 +3660,10 @@ mlir::LogicalResult CIRToLLVMSetBitfieldOpLowering::matchAndRewrite( } } + resultVal = createIntCast(rewriter, resultVal, + mlir::cast(resultTy), + info.getIsSigned()); + rewriter.replaceOp(op, resultVal); return mlir::success(); } diff --git a/clang/test/CIR/CodeGen/bitfields.c b/clang/test/CIR/CodeGen/bitfields.c index ded089655f59..5f13b424daa4 100644 --- a/clang/test/CIR/CodeGen/bitfields.c +++ b/clang/test/CIR/CodeGen/bitfields.c @@ -1,5 +1,7 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir // RUN: FileCheck --input-file=%t.cir %s +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s struct __long { struct __attribute__((__packed__)) { @@ -131,6 +133,16 @@ void createD() { D d = {1,2,3}; } +// check the -1 is stored to the ret value +// LLVM: define dso_local i32 {{@.*get_a.*}} +// LLVM: %[[V1:.*]] = alloca i32 +// LLVM: store i32 -1, ptr %[[V1]], align 4 +// LLVM: %[[V2:.*]] = load i32, ptr %[[V1]], align 4 +// LLVM: ret i32 %[[V2:.*]] +int get_a(T *t) { + return (t->a = 7); +} + typedef struct { int x : 15; int y ;