Skip to content

Commit 1468ac4

Browse files
authored
[CIR][CIRGen][TBAA] Add support for scalar types (#1329)
This patch introduces support for TBAA with scalar types. By encoding the type name in the CIR, we address the limitation of distinguishing between different C++ types that map to the same CIR type. For example, long and long long types. see #1241
1 parent c4409e1 commit 1468ac4

24 files changed

+788
-56
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
170170
/*alignment=*/intAttr,
171171
/*mem_order=*/
172172
cir::MemOrderAttr{},
173-
/*tbaa=*/mlir::ArrayAttr{});
173+
/*tbaa=*/cir::TBAAAttr{});
174174
}
175175

176176
mlir::Value createAlignedLoad(mlir::Location loc, mlir::Value ptr,
@@ -357,7 +357,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
357357
val.getType())
358358
dst = createPtrBitcast(dst, val.getType());
359359
return create<cir::StoreOp>(loc, val, dst, _volatile, align, order,
360-
/*tbaa=*/mlir::ArrayAttr{});
360+
/*tbaa=*/cir::TBAAAttr{});
361361
}
362362

363363
mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType,
@@ -405,7 +405,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
405405
cir::CopyOp createCopy(mlir::Value dst, mlir::Value src,
406406
bool isVolatile = false) {
407407
return create<cir::CopyOp>(dst.getLoc(), dst, src, isVolatile,
408-
/*tbaa=*/mlir::ArrayAttr{});
408+
/*tbaa=*/cir::TBAAAttr{});
409409
}
410410

411411
cir::MemCpyOp createMemCpy(mlir::Location loc, mlir::Value dst,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ include "clang/CIR/Interfaces/ASTAttrInterfaces.td"
2424
// CIR Attrs
2525
//===----------------------------------------------------------------------===//
2626

27-
class CIR_Attr<string name, string attrMnemonic, list<Trait> traits = []>
28-
: AttrDef<CIR_Dialect, name, traits> {
27+
class CIR_Attr<string name, string attrMnemonic, list<Trait> traits = [],
28+
string baseCppClass = "::mlir::Attribute">
29+
: AttrDef<CIR_Dialect, name, traits, baseCppClass> {
2930
let mnemonic = attrMnemonic;
3031
}
3132

@@ -1323,8 +1324,7 @@ def GlobalAnnotationValuesAttr : CIR_Attr<"GlobalAnnotationValues",
13231324
let genVerifyDecl = 1;
13241325
}
13251326

1326-
def CIR_TBAAAttr : CIR_Attr<"TBAA", "tbaa", []> {
1327-
}
1327+
include "clang/CIR/Dialect/IR/CIRTBAAAttrs.td"
13281328

13291329
include "clang/CIR/Dialect/IR/CIROpenCLAttrs.td"
13301330
include "clang/CIR/Dialect/IR/CIRCUDAAttrs.td"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ def LoadOp : CIR_Op<"load", [
589589
UnitAttr:$is_volatile,
590590
OptionalAttr<I64Attr>:$alignment,
591591
OptionalAttr<MemOrder>:$mem_order,
592-
OptionalAttr<ArrayAttr>:$tbaa
592+
OptionalAttr<CIR_AnyTBAAAttr>:$tbaa
593593
);
594594
let results = (outs CIR_AnyType:$result);
595595

@@ -658,7 +658,7 @@ def StoreOp : CIR_Op<"store", [
658658
UnitAttr:$is_volatile,
659659
OptionalAttr<I64Attr>:$alignment,
660660
OptionalAttr<MemOrder>:$mem_order,
661-
OptionalAttr<ArrayAttr>:$tbaa);
661+
OptionalAttr<CIR_AnyTBAAAttr>:$tbaa);
662662

663663
let assemblyFormat = [{
664664
(`volatile` $is_volatile^)?
@@ -4121,7 +4121,7 @@ def CopyOp : CIR_Op<"copy",
41214121
let arguments = (ins Arg<CIR_PointerType, "", [MemWrite]>:$dst,
41224122
Arg<CIR_PointerType, "", [MemRead]>:$src,
41234123
UnitAttr:$is_volatile,
4124-
OptionalAttr<ArrayAttr>:$tbaa);
4124+
OptionalAttr<CIR_TBAAAttr>:$tbaa);
41254125
let summary = "Copies contents from a CIR pointer to another";
41264126
let description = [{
41274127
Given two CIR pointers, `src` and `dst`, `cir.copy` will copy the memory
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===----------------------------------------------------------------------===//
2+
// TBAAAttr
3+
//===----------------------------------------------------------------------===//
4+
5+
def CIR_TBAAAttr : CIR_Attr<"TBAA", "tbaa", []> {
6+
let summary = "CIR dialect TBAA base attribute";
7+
}
8+
9+
//===----------------------------------------------------------------------===//
10+
// TBAAOmnipotentCharAttr
11+
//===----------------------------------------------------------------------===//
12+
13+
def CIR_TBAAOmnipotentChar
14+
: CIR_Attr<"TBAAOmnipotentChar", "tbaa_omnipotent_char", [], "TBAAAttr"> {
15+
let summary = "Describes a special scalar type, the omnipotent char type.";
16+
}
17+
18+
//===----------------------------------------------------------------------===//
19+
// TBAAScalarAttr
20+
//===----------------------------------------------------------------------===//
21+
22+
def CIR_TBAAScalarAttr : CIR_Attr<"TBAAScalar", "tbaa_scalar", [], "TBAAAttr"> {
23+
let summary = "Describes a scalar type in TBAA with an identifier.";
24+
25+
let parameters = (ins StringRefParameter<> : $id, CIR_AnyType : $type);
26+
27+
let description = [{
28+
Define a TBAA scalar attribute.
29+
30+
Example:
31+
```mlir
32+
// CIR_TBAAScalarAttr
33+
#tbaa_scalar = #cir.tbaa_scalar<id = "int", type = !s32i>
34+
#tbaa_scalar1 = #cir.tbaa_scalar<id = "long long", type = !s64i>
35+
```
36+
37+
See the following link for more details:
38+
https://llvm.org/docs/LangRef.html#tbaa-metadata
39+
}];
40+
41+
let assemblyFormat = "`<` struct(params) `>`";
42+
}
43+
44+
def CIR_TBAATagAttr : CIR_Attr<"TBAATag", "tbaa_tag", [], "TBAAAttr"> {
45+
let parameters = (ins CIR_TBAAAttr
46+
: $base, CIR_TBAAAttr
47+
: $access, "int64_t"
48+
: $offset);
49+
50+
let assemblyFormat = "`<` struct(params) `>`";
51+
}
52+
53+
def CIR_AnyTBAAAttr : AnyAttrOf<[
54+
CIR_TBAAAttr,
55+
CIR_TBAAOmnipotentChar,
56+
CIR_TBAAScalarAttr,
57+
CIR_TBAATagAttr
58+
]>;

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ struct MissingFeatures {
5858
// sanitizer related type check features
5959
static bool emitTypeCheck() { return false; }
6060
static bool tbaa() { return false; }
61-
static bool tbaa_struct() { return false; }
61+
static bool tbaaStruct() { return false; }
62+
static bool tbaaTagForStruct() { return false; }
63+
static bool tbaaTagForEnum() { return false; }
64+
static bool tbaaTagForBitInt() { return false; }
65+
static bool tbaaVTablePtr() { return false; }
66+
static bool tbaaIncompleteType() { return false; }
67+
static bool tbaaMergeTBAAInfo() { return false; }
68+
static bool tbaaMayAlias() { return false; }
69+
static bool tbaaNewStructPath() { return false; }
70+
static bool tbaaPointer() { return false; }
6271
static bool emitNullabilityCheck() { return false; }
6372
static bool ptrAuth() { return false; }
6473

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
868868
return create<cir::LoadOp>(
869869
loc, addr.getElementType(), addr.getPointer(), /*isDeref=*/false,
870870
/*is_volatile=*/isVolatile, /*alignment=*/mlir::IntegerAttr{},
871-
/*mem_order=*/cir::MemOrderAttr{}, /*tbaa=*/mlir::ArrayAttr{});
871+
/*mem_order=*/cir::MemOrderAttr{}, /*tbaa=*/cir::TBAAAttr{});
872872
}
873873

874874
mlir::Value createAlignedLoad(mlir::Location loc, mlir::Type ty,

clang/lib/CIR/CodeGen/CIRGenExprAgg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,7 @@ void CIRGenFunction::emitAggregateCopy(LValue Dest, LValue Src, QualType Ty,
17221722
// Determine the metadata to describe the position of any padding in this
17231723
// memcpy, as well as the TBAA tags for the members of the struct, in case
17241724
// the optimizer wishes to expand it in to scalar memory operations.
1725-
assert(!cir::MissingFeatures::tbaa_struct() && "tbaa.struct NYI");
1725+
assert(!cir::MissingFeatures::tbaaStruct() && "tbaa.struct NYI");
17261726
if (CGM.getCodeGenOpts().NewStructPathTBAA) {
17271727
TBAAAccessInfo TBAAInfo = CGM.mergeTBAAInfoForMemoryTransfer(
17281728
Dest.getTBAAInfo(), Src.getTBAAInfo());

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4096,7 +4096,7 @@ cir::TBAAAttr CIRGenModule::getTBAABaseTypeInfo(QualType QTy) {
40964096
return tbaa->getBaseTypeInfo(QTy);
40974097
}
40984098

4099-
mlir::ArrayAttr CIRGenModule::getTBAAAccessTagInfo(TBAAAccessInfo tbaaInfo) {
4099+
cir::TBAAAttr CIRGenModule::getTBAAAccessTagInfo(TBAAAccessInfo tbaaInfo) {
41004100
if (!tbaa) {
41014101
return nullptr;
41024102
}

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ class CIRGenModule : public CIRGenTypeCache {
576576
/// type is not suitable for use in TBAA access tags.
577577
cir::TBAAAttr getTBAABaseTypeInfo(QualType QTy);
578578

579-
mlir::ArrayAttr getTBAAAccessTagInfo(TBAAAccessInfo tbaaInfo);
579+
cir::TBAAAttr getTBAAAccessTagInfo(TBAAAccessInfo tbaaInfo);
580580

581581
/// Get merged TBAA information for the purposes of type casts.
582582
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,

0 commit comments

Comments
 (0)