Skip to content

Commit 1ff99df

Browse files
seven-milelanza
authored andcommitted
[CIR][Dialect][NFC] Fix double white spaces in cir.global assembly (llvm#1096)
Following llvm#1009 and llvm#1028, this PR removes the double white spaces in the assembly format of `cir.global` op. It's basically some `mlir-tablegen`-builtin magic: With `constBuilderCall` specified, we can apply `DefaultValuedAttr` with any default value we can construct from constant values. Then we can easily omit the default in assembly. Hence, we don't need to compromise anything for the wrapper attribute `cir::VisibilityAttr`. Similarly to llvm#1009, an empty literal ``` `` ``` is used to omit the leading space emitted by inner attribute. The test case `visibility-attribute.c` is modified to save the intermediate CIR to disk and reflect the effects. Double whitespaces in other test files are removed.
1 parent 12ae66a commit 1ff99df

25 files changed

+55
-60
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,9 @@ def VisibilityAttr : CIR_Attr<"Visibility", "visibility"> {
10161016

10171017
let skipDefaultBuilders = 1;
10181018

1019+
// Make DefaultValuedAttr accept VisibilityKind as default value ($0).
1020+
let constBuilderCall = "cir::VisibilityAttr::get($_builder.getContext(), $0)";
1021+
10191022
let extraClassDeclaration = [{
10201023
bool isDefault() const { return getValue() == VisibilityKind::Default; };
10211024
bool isHidden() const { return getValue() == VisibilityKind::Hidden; };

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,10 @@ def GlobalOp : CIR_Op<"global",
23872387
// TODO: sym_visibility can possibly be represented by implementing the
23882388
// necessary Symbol's interface in terms of linkage instead.
23892389
let arguments = (ins SymbolNameAttr:$sym_name,
2390-
VisibilityAttr:$global_visibility,
2390+
DefaultValuedAttr<
2391+
VisibilityAttr,
2392+
"VisibilityKind::Default"
2393+
>:$global_visibility,
23912394
OptionalAttr<StrAttr>:$sym_visibility,
23922395
TypeAttr:$sym_type,
23932396
Arg<GlobalLinkageKind, "linkage type">:$linkage,
@@ -2405,7 +2408,7 @@ def GlobalOp : CIR_Op<"global",
24052408
let regions = (region AnyRegion:$ctorRegion, AnyRegion:$dtorRegion);
24062409
let assemblyFormat = [{
24072410
($sym_visibility^)?
2408-
custom<OmitDefaultVisibility>($global_visibility)
2411+
(`` $global_visibility^)?
24092412
(`constant` $constant^)?
24102413
$linkage
24112414
(`comdat` $comdat^)?

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,6 @@ static void printOmittedTerminatorRegion(mlir::OpAsmPrinter &printer,
285285
/*printBlockTerminators=*/!omitRegionTerm(region));
286286
}
287287

288-
static mlir::ParseResult
289-
parseOmitDefaultVisibility(mlir::OpAsmParser &parser,
290-
cir::VisibilityAttr &visibility) {
291-
parseVisibilityAttr(parser, visibility);
292-
return success();
293-
}
294-
295-
static void printOmitDefaultVisibility(mlir::OpAsmPrinter &printer,
296-
cir::GlobalOp &op,
297-
cir::VisibilityAttr visibility) {
298-
printVisibilityAttr(printer, visibility);
299-
}
300-
301288
//===----------------------------------------------------------------------===//
302289
// AllocaOp
303290
//===----------------------------------------------------------------------===//

clang/test/CIR/CallConvLowering/AArch64/struct.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ S init(S s) {
4040
return s;
4141
}
4242

43-
// CIR: cir.func no_proto @foo1
43+
// CIR: cir.func no_proto @foo1
4444
// CIR: %[[#V0:]] = cir.alloca !ty_S, !cir.ptr<!ty_S>, ["s"]
4545
// CIR: %[[#V1:]] = cir.alloca !ty_S, !cir.ptr<!ty_S>, ["tmp"] {alignment = 4 : i64}
4646
// CIR: %[[#V2:]] = cir.cast(bitcast, %[[#V0]] : !cir.ptr<!ty_S>), !cir.ptr<!u64i>
@@ -143,7 +143,7 @@ S2 init2(S2 s) {
143143
return s;
144144
}
145145

146-
// CIR: cir.func no_proto @foo3()
146+
// CIR: cir.func no_proto @foo3()
147147
// CIR: %[[#V0:]] = cir.alloca !ty_S2, !cir.ptr<!ty_S2>, ["s"]
148148
// CIR: %[[#V1:]] = cir.alloca !ty_S2, !cir.ptr<!ty_S2>, ["tmp"] {alignment = 1 : i64}
149149
// CIR: %[[#V2:]] = cir.cast(bitcast, %[[#V0]] : !cir.ptr<!ty_S2>), !cir.ptr<!u16i>

clang/test/CIR/CodeGen/AArch64/neon-arith.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
float32_t test_vrndns_f32(float32_t a) {
2222
return vrndns_f32(a);
2323
}
24-
// CIR: cir.func internal private @vrndns_f32(%arg0: !cir.float {{.*}}) -> !cir.float
24+
// CIR: cir.func internal private @vrndns_f32(%arg0: !cir.float {{.*}}) -> !cir.float
2525
// CIR: cir.store %arg0, [[ARG_SAVE:%.*]] : !cir.float, !cir.ptr<!cir.float>
2626
// CIR: [[INTRIN_ARG:%.*]] = cir.load [[ARG_SAVE]] : !cir.ptr<!cir.float>, !cir.float
2727
// CIR: {{%.*}} = cir.llvm.intrinsic "roundeven.f32" [[INTRIN_ARG]] : (!cir.float)
@@ -43,7 +43,7 @@ float32x2_t test_vrnda_f32(float32x2_t a) {
4343
return vrnda_f32(a);
4444
}
4545

46-
// CIR: cir.func internal private @vrnda_f32(%arg0: !cir.vector<!cir.float x 2>
46+
// CIR: cir.func internal private @vrnda_f32(%arg0: !cir.vector<!cir.float x 2>
4747
// CIR: cir.store %arg0, [[ARG_SAVE:%.*]] : !cir.vector<!cir.float x 2>, !cir.ptr<!cir.vector<!cir.float x 2>>
4848
// CIR: [[INTRIN_ARG:%.*]] = cir.load [[ARG_SAVE]] : !cir.ptr<!cir.vector<!cir.float x 2>>, !cir.vector<!cir.float x 2>
4949
// CIR: [[INTRIN_ARG_CAST:%.*]] = cir.cast(bitcast, [[INTRIN_ARG]] : !cir.vector<!cir.float x 2>), !cir.vector<!s8i x 8>
@@ -67,7 +67,7 @@ float32x4_t test_vrndaq_f32(float32x4_t a) {
6767
return vrndaq_f32(a);
6868
}
6969

70-
// CIR: cir.func internal private @vrndaq_f32(%arg0: !cir.vector<!cir.float x 4>
70+
// CIR: cir.func internal private @vrndaq_f32(%arg0: !cir.vector<!cir.float x 4>
7171
// CIR: cir.store %arg0, [[ARG_SAVE:%.*]] : !cir.vector<!cir.float x 4>, !cir.ptr<!cir.vector<!cir.float x 4>>
7272
// CIR: [[INTRIN_ARG:%.*]] = cir.load [[ARG_SAVE]] : !cir.ptr<!cir.vector<!cir.float x 4>>, !cir.vector<!cir.float x 4>
7373
// CIR: [[INTRIN_ARG_CAST:%.*]] = cir.cast(bitcast, [[INTRIN_ARG]] : !cir.vector<!cir.float x 4>), !cir.vector<!s8i x 16>

clang/test/CIR/CodeGen/annotations-var.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-llvm %s -o %t.ll
44
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
55

6-
// CIR-DAG: cir.global external @globalvar = #cir.int<3> : !s32i [#cir.annotation<name = "globalvar_ann_0", args = []>] {alignment = 4 : i64}
7-
// CIR-DAG: cir.global external @globalvar2 = #cir.int<2> : !s32i [#cir.annotation<name = "common_ann", args = ["os", 21 : i32]>] {alignment = 4 : i64}
6+
// CIR-DAG: cir.global external @globalvar = #cir.int<3> : !s32i [#cir.annotation<name = "globalvar_ann_0", args = []>] {alignment = 4 : i64}
7+
// CIR-DAG: cir.global external @globalvar2 = #cir.int<2> : !s32i [#cir.annotation<name = "common_ann", args = ["os", 21 : i32]>] {alignment = 4 : i64}
88

99
// LLVM-DAG: @.str.annotation = private unnamed_addr constant [15 x i8] c"localvar_ann_0\00", section "llvm.metadata"
1010
// LLVM-DAG: @.str.1.annotation = private unnamed_addr constant [{{[0-9]+}} x i8] c"{{.*}}annotations-var.c\00", section "llvm.metadata"

clang/test/CIR/CodeGen/array-init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o - | FileCheck %s
22

3-
// CHECK-DAG: cir.global "private" constant cir_private @__const.foo.bar = #cir.const_array<[#cir.fp<9.000000e+00> : !cir.double, #cir.fp<8.000000e+00> : !cir.double, #cir.fp<7.000000e+00> : !cir.double]> : !cir.array<!cir.double x 3>
3+
// CHECK-DAG: cir.global "private" constant cir_private @__const.foo.bar = #cir.const_array<[#cir.fp<9.000000e+00> : !cir.double, #cir.fp<8.000000e+00> : !cir.double, #cir.fp<7.000000e+00> : !cir.double]> : !cir.array<!cir.double x 3>
44
typedef struct {
55
int a;
66
long b;

clang/test/CIR/CodeGen/attribute-annotate-multiple.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ void bar() __attribute__((annotate("withargfunc", "os", 22))) {
1818

1919
// BEFORE: module @{{.*}}attribute-annotate-multiple.cpp" attributes {cir.lang =
2020

21-
// BEFORE: cir.global external @a = #cir.ptr<null> : !cir.ptr<!cir.double>
21+
// BEFORE: cir.global external @a = #cir.ptr<null> : !cir.ptr<!cir.double>
2222
// BEFORE-SAME: [#cir.annotation<name = "withargs", args = ["21", 12 : i32]>]
23-
// BEFORE: cir.global external @b = #cir.ptr<null> : !cir.ptr<!s32i>
23+
// BEFORE: cir.global external @b = #cir.ptr<null> : !cir.ptr<!s32i>
2424
// BEFORE-SAME: [#cir.annotation<name = "withargs", args = ["21", 12 : i32]>]
25-
// BEFORE: cir.global external @c = #cir.ptr<null> : !cir.ptr<!void>
25+
// BEFORE: cir.global external @c = #cir.ptr<null> : !cir.ptr<!void>
2626
// BEFORE-SAME: [#cir.annotation<name = "noargvar", args = []>]
2727
// BEFORE: cir.global external @tile = #cir.int<7> : !s32i
2828
// BEFORE-SAME: #cir.annotation<name = "cir.aie.device.tile", args = [42 : i8]>]

clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ const int &compat_use_after_redecl1 = compat::c;
2626
const int &compat_use_after_redecl2 = compat::d;
2727
const int &compat_use_after_redecl3 = compat::g;
2828

29-
// CIR: cir.global constant weak_odr comdat @_ZN6compat1bE = #cir.int<2> : !s32i {alignment = 4 : i64}
30-
// CIR: cir.global constant weak_odr comdat @_ZN6compat1aE = #cir.int<1> : !s32i {alignment = 4 : i64}
31-
// CIR: cir.global constant weak_odr comdat @_ZN6compat1cE = #cir.int<3> : !s32i {alignment = 4 : i64}
32-
// CIR: cir.global constant external @_ZN6compat1eE = #cir.int<5> : !s32i {alignment = 4 : i64}
33-
// CIR: cir.global constant weak_odr comdat @_ZN6compat1fE = #cir.int<6> : !s32i {alignment = 4 : i64}
34-
// CIR: cir.global constant linkonce_odr comdat @_ZN6compat1dE = #cir.int<4> : !s32i {alignment = 4 : i64}
35-
// CIR: cir.global constant linkonce_odr comdat @_ZN6compat1gE = #cir.int<7> : !s32i {alignment = 4 : i64}
29+
// CIR: cir.global constant weak_odr comdat @_ZN6compat1bE = #cir.int<2> : !s32i {alignment = 4 : i64}
30+
// CIR: cir.global constant weak_odr comdat @_ZN6compat1aE = #cir.int<1> : !s32i {alignment = 4 : i64}
31+
// CIR: cir.global constant weak_odr comdat @_ZN6compat1cE = #cir.int<3> : !s32i {alignment = 4 : i64}
32+
// CIR: cir.global constant external @_ZN6compat1eE = #cir.int<5> : !s32i {alignment = 4 : i64}
33+
// CIR: cir.global constant weak_odr comdat @_ZN6compat1fE = #cir.int<6> : !s32i {alignment = 4 : i64}
34+
// CIR: cir.global constant linkonce_odr comdat @_ZN6compat1dE = #cir.int<4> : !s32i {alignment = 4 : i64}
35+
// CIR: cir.global constant linkonce_odr comdat @_ZN6compat1gE = #cir.int<7> : !s32i {alignment = 4 : i64}
3636

3737
// LLVM: $_ZN6compat1bE = comdat any
3838
// LLVM: $_ZN6compat1aE = comdat any

clang/test/CIR/CodeGen/global-new.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ e *g = new e(0);
1414

1515
// CIR_BEFORE: ![[ty:.*]] = !cir.struct<struct "e" {!u8i}
1616

17-
// CIR_BEFORE: cir.global external @g = ctor : !cir.ptr<![[ty]]> {
17+
// CIR_BEFORE: cir.global external @g = ctor : !cir.ptr<![[ty]]> {
1818
// CIR_BEFORE: %[[GlobalAddr:.*]] = cir.get_global @g : !cir.ptr<!cir.ptr<![[ty]]>>
1919
// CIR_BEFORE: %[[Size:.*]] = cir.const #cir.int<1> : !u64i
2020
// CIR_BEFORE: %[[NewAlloc:.*]] = cir.call @_Znwm(%[[Size]]) : (!u64i) -> !cir.ptr<!void>
@@ -37,7 +37,7 @@ e *g = new e(0);
3737
// CIR_EH: cir.resume
3838
// CIR_EH: }]
3939

40-
// CIR_FLAT_EH: cir.func internal private @__cxx_global_var_init()
40+
// CIR_FLAT_EH: cir.func internal private @__cxx_global_var_init()
4141
// CIR_FLAT_EH: ^bb3:
4242
// CIR_FLAT_EH: %exception_ptr, %type_id = cir.eh.inflight_exception
4343
// CIR_FLAT_EH: cir.call @_ZdlPvm({{.*}}) : (!cir.ptr<!void>, !u64i) -> ()

0 commit comments

Comments
 (0)