Skip to content

Commit cb40fcc

Browse files
ivanmurashkolanza
authored andcommitted
[CIR][CIRGen] Inline variables processing (llvm#794)
There is an implementation for inline variables processing at CIR. The LIT test was taken from clang's cxx1z-inline-variables.cpp where the same functionality is tested for Clang Code generation. The test can be run as follows ``` bin/llvm-lit -v ../clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp ``` Note: the pull request also contains a formatting change for two files: - `clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp` - `clang/lib/CIR/Dialect/Transforms/TargetLowering/Targets/X86.cpp`
1 parent 253d1e3 commit cb40fcc

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,11 @@ void CIRGenModule::buildGlobal(GlobalDecl GD) {
511511
assert(0 && "OMPDeclareTargetDeclAttr NYI");
512512
}
513513
}
514-
// If this declaration may have caused an inline variable definition
515-
// to change linkage, make sure that it's emitted.
516-
// TODO(cir): probably use GetAddrOfGlobalVar(VD) below?
517-
assert((astCtx.getInlineVariableDefinitionKind(VD) !=
518-
ASTContext::InlineVariableDefinitionKind::Strong) &&
519-
"not implemented");
514+
// If this declaration may have caused an inline variable definition to
515+
// change linkage, make sure that it's emitted.
516+
if (astCtx.getInlineVariableDefinitionKind(VD) ==
517+
ASTContext::InlineVariableDefinitionKind::Strong)
518+
getAddrOfGlobalVar(VD);
520519
return;
521520
}
522521
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ llvm::Align CIRDataLayout::getAlignment(mlir::Type Ty, bool abiOrPref) const {
185185

186186
// Fetch type alignment from MLIR's data layout.
187187
unsigned align = abiOrPref ? layout.getTypeABIAlignment(Ty)
188-
: layout.getTypePreferredAlignment(Ty);
188+
: layout.getTypePreferredAlignment(Ty);
189189
return llvm::Align(align);
190190
}
191191

clang/lib/CIR/Dialect/Transforms/TargetLowering/Targets/X86.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ void X86_64ABIInfo::classify(Type Ty, uint64_t OffsetBase, Class &Lo, Class &Hi,
235235
} else if (isa<IntType>(Ty)) {
236236

237237
// FIXME(cir): Clang's BuiltinType::Kind allow comparisons (GT, LT, etc).
238-
// We should implement this in CIR to simplify the conditions below. Hence,
239-
// Comparisons below might not be truly equivalent to the ones in Clang.
238+
// We should implement this in CIR to simplify the conditions below.
239+
// Hence, Comparisons below might not be truly equivalent to the ones in
240+
// Clang.
240241
if (isa<IntType>(Ty)) {
241242
Current = Class::Integer;
242243
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck -check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple aarch64-none-linux-android21 -fclangir -emit-llvm %s -o %t.ll
4+
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
5+
6+
// For compatibility with C++11 and C++14, an out-of-line declaration of a
7+
// static constexpr local variable promotes the variable to weak_odr.
8+
struct compat {
9+
static constexpr int a = 1;
10+
static constexpr int b = 2;
11+
static constexpr int c = 3;
12+
static inline constexpr int d = 4;
13+
static const int e = 5;
14+
static const int f = 6;
15+
static const int g = 7;
16+
};
17+
const int &compat_use_before_redecl = compat::b;
18+
const int compat::a;
19+
const int compat::b;
20+
const int compat::c;
21+
const int compat::d;
22+
const int compat::e;
23+
constexpr int compat::f;
24+
constexpr inline int compat::g;
25+
const int &compat_use_after_redecl1 = compat::c;
26+
const int &compat_use_after_redecl2 = compat::d;
27+
const int &compat_use_after_redecl3 = compat::g;
28+
29+
// CIR: cir.global weak_odr @_ZN6compat1bE = #cir.int<2> : !s32i
30+
// CIR: cir.global weak_odr @_ZN6compat1aE = #cir.int<1> : !s32i
31+
// CIR: cir.global weak_odr @_ZN6compat1cE = #cir.int<3> : !s32i
32+
// CIR: cir.global external @_ZN6compat1eE = #cir.int<5> : !s32i
33+
// CIR: cir.global weak_odr @_ZN6compat1fE = #cir.int<6> : !s32i
34+
// CIR: cir.global linkonce_odr @_ZN6compat1dE = #cir.int<4> : !s32i
35+
// CIR: cir.global linkonce_odr @_ZN6compat1gE = #cir.int<7> : !s32i
36+
37+
// LLVM: @_ZN6compat1bE = weak_odr global i32 2
38+
// LLVM: @_ZN6compat1aE = weak_odr global i32 1
39+
// LLVM: @_ZN6compat1cE = weak_odr global i32 3
40+
// LLVM: @_ZN6compat1eE = global i32 5
41+
// LLVM: @_ZN6compat1fE = weak_odr global i32 6
42+
// LLVM: @_ZN6compat1dE = linkonce_odr global i32 4
43+
// LLVM: @_ZN6compat1gE = linkonce_odr global i32 7
44+

0 commit comments

Comments
 (0)