Skip to content

Commit 78e0c76

Browse files
[CIR][NFC] Add example for get_bitfield with volatile qualifier (#147828)
The example demonstrates how `get_bitfield` is emitted when accessing a bitfield declared as `volatile`.
1 parent 76eead1 commit 78e0c76

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,9 @@ def GetBitfieldOp : CIR_Op<"get_bitfield"> {
17731773

17741774
A unit attribute `volatile` can be used to indicate a volatile load of the
17751775
bitfield.
1776+
```mlir
1777+
cir.get_bitfield(#bfi, %0 {is_volatile} : !cir.ptr<!u64i>) -> !s32i
1778+
```
17761779

17771780
Example:
17781781
Suppose we have a struct with multiple bitfields stored in

clang/test/CIR/CodeGen/bitfields.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,34 @@ typedef struct {
213213
int c;
214214
} V;
215215

216+
void get_volatile(V* v) {
217+
v->b = 3;
218+
}
219+
220+
// CIR: cir.func dso_local @get_volatile
221+
// CIR: [[TMP0:%.*]] = cir.alloca !cir.ptr<!rec_V>, !cir.ptr<!cir.ptr<!rec_V>>, ["v", init] {alignment = 8 : i64}
222+
// CIR: [[TMP1:%.*]] = cir.const #cir.int<3> : !s32i
223+
// CIR: [[TMP2:%.*]] = cir.load align(8) [[TMP0]] : !cir.ptr<!cir.ptr<!rec_V>>, !cir.ptr<!rec_V>
224+
// CIR: [[TMP3:%.*]] = cir.get_member [[TMP2]][0] {name = "b"} : !cir.ptr<!rec_V> -> !cir.ptr<!u64i>
225+
// CIR: [[TMP4:%.*]] = cir.set_bitfield(#bfi_b, [[TMP3]] : !cir.ptr<!u64i>, [[TMP1]] : !s32i) {is_volatile} -> !s32i
226+
227+
// LLVM: define dso_local void @get_volatile
228+
// LLVM: [[TMP0:%.*]] = alloca ptr, i64 1, align 8
229+
// LLVM: [[TMP1:%.*]] = load ptr, ptr [[TMP0]], align 8
230+
// LLVM: [[TMP2:%.*]] = getelementptr %struct.V, ptr [[TMP1]], i32 0, i32 0
231+
// LLVM: [[TMP3:%.*]] = load volatile i64, ptr [[TMP2]], align 8
232+
// LLVM: [[TMP4:%.*]] = and i64 [[TMP3]], -1095216660481
233+
// LLVM: [[TMP5:%.*]] = or i64 [[TMP4]], 12884901888
234+
// LLVM: store volatile i64 [[TMP5]], ptr [[TMP2]], align 8
235+
236+
// OCGC: define dso_local void @get_volatile
237+
// OCGC: [[TMP0:%.*]] = alloca ptr, align 8
238+
// OCGC: [[TMP1:%.*]] = load ptr, ptr [[TMP0]], align 8
239+
// OCGC: [[TMP2:%.*]] = load volatile i64, ptr [[TMP1]], align 4
240+
// OCGC: [[TMP3:%.*]] = and i64 [[TMP2]], -1095216660481
241+
// OCGC: [[TMP4:%.*]] = or i64 [[TMP3]], 12884901888
242+
// OCGC: store volatile i64 [[TMP4]], ptr [[TMP1]], align 4
243+
216244
void set_volatile(V* v) {
217245
v->b = 3;
218246
}

0 commit comments

Comments
 (0)