Skip to content

Commit a434102

Browse files
bcardosolopeslanza
authored andcommitted
Revert "[CIR][LLVMLowering] Lower cir.objectsize (#545)"
This reverts commit 87a61f3. It's deleting code it isn't supposed to touch.
1 parent 5c953b0 commit a434102

File tree

10 files changed

+82
-83
lines changed

10 files changed

+82
-83
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,24 +1776,31 @@ def GetGlobalOp : CIR_Op<"get_global",
17761776
[Pure, DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
17771777
let summary = "Get the address of a global variable";
17781778
let description = [{
1779-
The `cir.get_global` operation retrieves the address pointing to a
1780-
named global variable. If the global variable is marked constant, writing
1781-
to the resulting address (such as through a `cir.store` operation) is
1782-
undefined. Resulting type must always be a `!cir.ptr<...>` type.
1779+
The `cir.get_global` operation retrieves the address pointing to a
1780+
named global variable. If the global variable is marked constant, writing
1781+
to the resulting address (such as through a `cir.store` operation) is
1782+
undefined. Resulting type must always be a `!cir.ptr<...>` type.
17831783

1784-
Example:
1784+
Addresses of thread local globals can only be retrieved if this operation
1785+
is marked `thread_local`, which indicates the address isn't constant.
17851786

1786-
```mlir
1787-
%x = cir.get_global @foo : !cir.ptr<i32>
1788-
```
1787+
Example:
1788+
```mlir
1789+
%x = cir.get_global @foo : !cir.ptr<i32>
1790+
...
1791+
%y = cir.get_global thread_local @batata : !cir.ptr<i32>
1792+
```
17891793
}];
17901794

1791-
let arguments = (ins FlatSymbolRefAttr:$name);
1795+
let arguments = (ins FlatSymbolRefAttr:$name, UnitAttr:$tls);
17921796
let results = (outs Res<CIR_PointerType, "", []>:$addr);
17931797

17941798
// FIXME: we should not be printing `cir.ptr` below, that should come
17951799
// from the pointer type directly.
1796-
let assemblyFormat = "$name `:` `cir.ptr` type($addr) attr-dict";
1800+
let assemblyFormat = [{
1801+
(`thread_local` $tls^)?
1802+
$name `:` `cir.ptr` type($addr) attr-dict
1803+
}];
17971804

17981805
// `GetGlobalOp` is fully verified by its traits.
17991806
let hasVerifier = 0;

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,11 @@ class CIRGenBuilderTy : public CIRBaseBuilderTy {
697697
return create<mlir::cir::GlobalOp>(loc, uniqueName, type, isConst, linkage);
698698
}
699699

700-
mlir::Value createGetGlobal(mlir::cir::GlobalOp global) {
701-
return create<mlir::cir::GetGlobalOp>(
702-
global.getLoc(), getPointerTo(global.getSymType()), global.getName());
700+
mlir::Value createGetGlobal(mlir::cir::GlobalOp global,
701+
bool threadLocal = false) {
702+
return create<mlir::cir::GetGlobalOp>(global.getLoc(),
703+
getPointerTo(global.getSymType()),
704+
global.getName(), threadLocal);
703705
}
704706

705707
mlir::Value createGetBitfield(mlir::Location loc, mlir::Type resultType,

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,10 @@ static LValue buildGlobalVarDeclLValue(CIRGenFunction &CGF, const Expr *E,
720720
if (CGF.getLangOpts().OpenMP)
721721
llvm_unreachable("not implemented");
722722

723+
// Traditional LLVM codegen handles thread local separately, CIR handles
724+
// as part of getAddrOfGlobalVar.
723725
auto V = CGF.CGM.getAddrOfGlobalVar(VD);
724726

725-
if (VD->getTLSKind() != VarDecl::TLS_None)
726-
llvm_unreachable("NYI");
727-
728727
auto RealVarTy = CGF.getTypes().convertTypeForMem(VD->getType());
729728
auto realPtrTy = CGF.getBuilder().getPointerTo(RealVarTy);
730729
if (realPtrTy != V.getType())

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,11 +837,12 @@ mlir::Value CIRGenModule::getAddrOfGlobalVar(const VarDecl *D, mlir::Type Ty,
837837
if (!Ty)
838838
Ty = getTypes().convertTypeForMem(ASTTy);
839839

840+
bool tlsAccess = D->getTLSKind() != VarDecl::TLS_None;
840841
auto g = buildGlobal(D, Ty, IsForDefinition);
841842
auto ptrTy =
842843
mlir::cir::PointerType::get(builder.getContext(), g.getSymType());
843-
return builder.create<mlir::cir::GetGlobalOp>(getLoc(D->getSourceRange()),
844-
ptrTy, g.getSymName());
844+
return builder.create<mlir::cir::GetGlobalOp>(
845+
getLoc(D->getSourceRange()), ptrTy, g.getSymName(), tlsAccess);
845846
}
846847

847848
mlir::cir::GlobalViewAttr

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,9 +1635,13 @@ GetGlobalOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
16351635
<< "' does not reference a valid cir.global or cir.func";
16361636

16371637
mlir::Type symTy;
1638-
if (auto g = dyn_cast<GlobalOp>(op))
1638+
if (auto g = dyn_cast<GlobalOp>(op)) {
16391639
symTy = g.getSymType();
1640-
else if (auto f = dyn_cast<FuncOp>(op))
1640+
// Verify that for thread local global access, the global needs to
1641+
// be marked with tls bits.
1642+
if (getTls() && !g.getTlsModel())
1643+
return emitOpError("access to global not marked thread local");
1644+
} else if (auto f = dyn_cast<FuncOp>(op))
16411645
symTy = f.getFunctionType();
16421646
else
16431647
llvm_unreachable("shall not get here");

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,16 @@ class CIRGetGlobalOpLowering
16271627

16281628
auto type = getTypeConverter()->convertType(op.getType());
16291629
auto symbol = op.getName();
1630-
rewriter.replaceOpWithNewOp<mlir::LLVM::AddressOfOp>(op, type, symbol);
1630+
mlir::Operation *newop =
1631+
rewriter.create<mlir::LLVM::AddressOfOp>(op.getLoc(), type, symbol);
1632+
1633+
if (op.getTls()) {
1634+
// Handle access to TLS via intrinsic.
1635+
newop = rewriter.create<mlir::LLVM::ThreadlocalAddressOp>(
1636+
op.getLoc(), type, newop->getResult(0));
1637+
}
1638+
1639+
rewriter.replaceOp(op, newop);
16311640
return mlir::success();
16321641
}
16331642
};
@@ -2292,36 +2301,6 @@ class CIRBitClrsbOpLowering
22922301
}
22932302
};
22942303

2295-
class CIRObjSizeOpLowering
2296-
: public mlir::OpConversionPattern<mlir::cir::ObjSizeOp> {
2297-
public:
2298-
using OpConversionPattern<mlir::cir::ObjSizeOp>::OpConversionPattern;
2299-
2300-
mlir::LogicalResult
2301-
matchAndRewrite(mlir::cir::ObjSizeOp op, OpAdaptor adaptor,
2302-
mlir::ConversionPatternRewriter &rewriter) const override {
2303-
auto llvmResTy = getTypeConverter()->convertType(op.getType());
2304-
auto loc = op->getLoc();
2305-
2306-
auto llvmIntrinNameAttr =
2307-
mlir::StringAttr::get(rewriter.getContext(), "llvm.objectsize");
2308-
mlir::cir::SizeInfoType kindInfo = op.getKind();
2309-
auto falseValue = rewriter.create<mlir::LLVM::ConstantOp>(
2310-
loc, rewriter.getI1Type(), false);
2311-
auto trueValue = rewriter.create<mlir::LLVM::ConstantOp>(
2312-
loc, rewriter.getI1Type(), true);
2313-
2314-
rewriter.replaceOpWithNewOp<mlir::LLVM::CallIntrinsicOp>(
2315-
op, llvmResTy, llvmIntrinNameAttr,
2316-
mlir::ValueRange{adaptor.getPtr(),
2317-
kindInfo == mlir::cir::SizeInfoType::max ? falseValue
2318-
: trueValue,
2319-
trueValue, op.getDynamic() ? trueValue : falseValue});
2320-
2321-
return mlir::LogicalResult::success();
2322-
}
2323-
};
2324-
23252304
class CIRBitClzOpLowering
23262305
: public mlir::OpConversionPattern<mlir::cir::BitClzOp> {
23272306
public:
@@ -3085,8 +3064,8 @@ void populateCIRToLLVMConversionPatterns(mlir::RewritePatternSet &patterns,
30853064
CIRVectorShuffleVecLowering, CIRStackSaveLowering,
30863065
CIRStackRestoreLowering, CIRUnreachableLowering, CIRTrapLowering,
30873066
CIRInlineAsmOpLowering, CIRSetBitfieldLowering, CIRGetBitfieldLowering,
3088-
CIRPrefetchLowering, CIRObjSizeOpLowering, CIRIsConstantOpLowering>(
3089-
converter, patterns.getContext());
3067+
CIRPrefetchLowering, CIRIsConstantOpLowering>(converter,
3068+
patterns.getContext());
30903069
}
30913070

30923071
namespace {

clang/test/CIR/CodeGen/pass-object-size.c

Lines changed: 0 additions & 29 deletions
This file was deleted.

clang/test/CIR/CodeGen/tls.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
44
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s
55

6+
extern __thread int b;
7+
int c(void) { return *&b; }
8+
// CIR: cir.global "private" external tls_dyn @b : !s32i
9+
// CIR: cir.func @c() -> !s32i
10+
// CIR: %[[TLS_ADDR:.*]] = cir.get_global thread_local @b : cir.ptr <!s32i>
11+
612
__thread int a;
713
// CIR: cir.global external tls_dyn @a = #cir.int<0> : !s32i
14+
15+
// LLVM: @b = external thread_local global i32
816
// LLVM: @a = thread_local global i32 0
17+
18+
// LLVM-LABEL: @c
19+
// LLVM: = call ptr @llvm.threadlocal.address.p0(ptr @b)

clang/test/CIR/IR/global.cir

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ module {
6363
cir.global external tls_local_dyn @model1 = #cir.int<0> : !s32i
6464
cir.global external tls_init_exec @model2 = #cir.int<0> : !s32i
6565
cir.global external tls_local_exec @model3 = #cir.int<0> : !s32i
66+
67+
cir.global "private" external tls_dyn @batata : !s32i
68+
cir.func @f35() {
69+
%0 = cir.get_global thread_local @batata : cir.ptr <!s32i>
70+
cir.return
71+
}
6672
}
6773

6874
// CHECK: cir.global external @a = #cir.int<3> : !s32i
@@ -91,4 +97,10 @@ module {
9197
// CHECK: cir.global external tls_dyn @model0 = #cir.int<0> : !s32i
9298
// CHECK: cir.global external tls_local_dyn @model1 = #cir.int<0> : !s32i
9399
// CHECK: cir.global external tls_init_exec @model2 = #cir.int<0> : !s32i
94-
// CHECK: cir.global external tls_local_exec @model3 = #cir.int<0> : !s32i
100+
// CHECK: cir.global external tls_local_exec @model3 = #cir.int<0> : !s32i
101+
102+
// CHECK: cir.global "private" external tls_dyn @batata : !s32i
103+
// CHECK: cir.func @f35() {
104+
// CHECK: %0 = cir.get_global thread_local @batata : cir.ptr <!s32i>
105+
// CHECK: cir.return
106+
// CHECK: }

clang/test/CIR/IR/invalid.cir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,3 +1034,16 @@ cir.func @bad_fetch(%x: !cir.ptr<!cir.float>, %y: !cir.float) -> () {
10341034
%12 = cir.atomic.fetch(xor, %x : !cir.ptr<!cir.float>, %y : !cir.float, seq_cst) : !cir.float
10351035
cir.return
10361036
}
1037+
1038+
// -----
1039+
1040+
!s32i = !cir.int<s, 32>
1041+
1042+
module {
1043+
cir.global "private" external @batata : !s32i
1044+
cir.func @f35() {
1045+
// expected-error@+1 {{access to global not marked thread local}}
1046+
%0 = cir.get_global thread_local @batata : cir.ptr <!s32i>
1047+
cir.return
1048+
}
1049+
}

0 commit comments

Comments
 (0)