diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index b7197afeb896..b35ef11c7782 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -814,6 +814,13 @@ void CIRGenModule::replaceGlobal(cir::GlobalOp Old, cir::GlobalOp New) { auto UseOpResultValue = GGO.getAddr(); UseOpResultValue.setType( cir::PointerType::get(&getMLIRContext(), NewTy)); + + mlir::OpBuilder::InsertionGuard guard(builder); + builder.setInsertionPointAfter(UserOp); + mlir::Type ptrTy = builder.getPointerTo(OldTy); + mlir::Value cast = + builder.createBitcast(GGO->getLoc(), UseOpResultValue, ptrTy); + UseOpResultValue.replaceAllUsesExcept(cast, {cast.getDefiningOp()}); } } } diff --git a/clang/test/CIR/CodeGen/bitfields.c b/clang/test/CIR/CodeGen/bitfields.c index 2671523cc4ca..ded089655f59 100644 --- a/clang/test/CIR/CodeGen/bitfields.c +++ b/clang/test/CIR/CodeGen/bitfields.c @@ -54,6 +54,7 @@ typedef struct { } U; // CHECK: !ty_D = !cir.struct +// CHECK: !ty_G = !cir.struct // CHECK: !ty_T = !cir.struct // CHECK: !ty_anon2E0_ = !cir.struct // CHECK: !ty_anon_struct = !cir.struct @@ -129,3 +130,20 @@ void createU() { void createD() { D d = {1,2,3}; } + +typedef struct { + int x : 15; + int y ; +} G; + +// CHECK: cir.global external @g = #cir.const_struct<{#cir.int<133> : !u8i, #cir.int<127> : !u8i, #cir.int<254> : !s32i}> : !ty_anon_struct +G g = { -123, 254UL}; + +// CHECK: cir.func {{.*@get_y}} +// CHECK: %[[V1:.*]] = cir.get_global @g : !cir.ptr +// CHECK: %[[V2:.*]] = cir.cast(bitcast, %[[V1]] : !cir.ptr), !cir.ptr +// CHECK: %[[V3:.*]] = cir.get_member %[[V2]][1] {name = "y"} : !cir.ptr -> !cir.ptr +// CHECK: cir.load %[[V3]] : !cir.ptr, !s32i +int get_y() { + return g.y; +}