From e125ad441918a1a7422fd9daa0228b17f519858e Mon Sep 17 00:00:00 2001 From: Andres Salamanca Date: Wed, 9 Jul 2025 15:59:52 -0500 Subject: [PATCH 1/2] [CIR][NFC] Add example for get_bitfield with volatile qualifier --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index ee0df83dd36e9..d5cdb5aa91251 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -1773,6 +1773,9 @@ def GetBitfieldOp : CIR_Op<"get_bitfield"> { A unit attribute `volatile` can be used to indicate a volatile load of the bitfield. + ```mlir + cir.get_bitfield(#bfi, %0 {is_volatile} : !cir.ptr) -> !s32i + ``` Example: Suppose we have a struct with multiple bitfields stored in From ad56adc3e3bf697ae4d11af20b8731ff3257b8a1 Mon Sep 17 00:00:00 2001 From: Andres Salamanca Date: Thu, 10 Jul 2025 17:14:32 -0500 Subject: [PATCH 2/2] Add the requested example --- clang/test/CIR/CodeGen/bitfields.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/clang/test/CIR/CodeGen/bitfields.c b/clang/test/CIR/CodeGen/bitfields.c index 63d301a20c38f..fc688fb4cdcaa 100644 --- a/clang/test/CIR/CodeGen/bitfields.c +++ b/clang/test/CIR/CodeGen/bitfields.c @@ -213,6 +213,34 @@ typedef struct { int c; } V; +void get_volatile(V* v) { + v->b = 3; +} + +// CIR: cir.func dso_local @get_volatile +// CIR: [[TMP0:%.*]] = cir.alloca !cir.ptr, !cir.ptr>, ["v", init] {alignment = 8 : i64} +// CIR: [[TMP1:%.*]] = cir.const #cir.int<3> : !s32i +// CIR: [[TMP2:%.*]] = cir.load align(8) [[TMP0]] : !cir.ptr>, !cir.ptr +// CIR: [[TMP3:%.*]] = cir.get_member [[TMP2]][0] {name = "b"} : !cir.ptr -> !cir.ptr +// CIR: [[TMP4:%.*]] = cir.set_bitfield(#bfi_b, [[TMP3]] : !cir.ptr, [[TMP1]] : !s32i) {is_volatile} -> !s32i + +// LLVM: define dso_local void @get_volatile +// LLVM: [[TMP0:%.*]] = alloca ptr, i64 1, align 8 +// LLVM: [[TMP1:%.*]] = load ptr, ptr [[TMP0]], align 8 +// LLVM: [[TMP2:%.*]] = getelementptr %struct.V, ptr [[TMP1]], i32 0, i32 0 +// LLVM: [[TMP3:%.*]] = load volatile i64, ptr [[TMP2]], align 8 +// LLVM: [[TMP4:%.*]] = and i64 [[TMP3]], -1095216660481 +// LLVM: [[TMP5:%.*]] = or i64 [[TMP4]], 12884901888 +// LLVM: store volatile i64 [[TMP5]], ptr [[TMP2]], align 8 + +// OCGC: define dso_local void @get_volatile +// OCGC: [[TMP0:%.*]] = alloca ptr, align 8 +// OCGC: [[TMP1:%.*]] = load ptr, ptr [[TMP0]], align 8 +// OCGC: [[TMP2:%.*]] = load volatile i64, ptr [[TMP1]], align 4 +// OCGC: [[TMP3:%.*]] = and i64 [[TMP2]], -1095216660481 +// OCGC: [[TMP4:%.*]] = or i64 [[TMP3]], 12884901888 +// OCGC: store volatile i64 [[TMP4]], ptr [[TMP1]], align 4 + void set_volatile(V* v) { v->b = 3; }