Skip to content

Commit 89e04a2

Browse files
[CIR] Upstream initial TBAA implementation
1 parent e6f60a6 commit 89e04a2

File tree

15 files changed

+445
-27
lines changed

15 files changed

+445
-27
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
205205
bool isVolatile = false, uint64_t alignment = 0) {
206206
mlir::IntegerAttr alignmentAttr = getAlignmentAttr(alignment);
207207
return cir::LoadOp::create(*this, loc, ptr, /*isDeref=*/false, isVolatile,
208-
alignmentAttr, cir::MemOrderAttr{});
208+
alignmentAttr, cir::MemOrderAttr{}, /*tbaa=*/mlir::ArrayAttr{});
209209
}
210210

211211
mlir::Value createAlignedLoad(mlir::Location loc, mlir::Value ptr,
@@ -321,14 +321,14 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
321321
/// Create a copy with inferred length.
322322
cir::CopyOp createCopy(mlir::Value dst, mlir::Value src,
323323
bool isVolatile = false) {
324-
return cir::CopyOp::create(*this, dst.getLoc(), dst, src, isVolatile);
324+
return cir::CopyOp::create(*this, dst.getLoc(), dst, src, isVolatile, /*tbaa=*/mlir::ArrayAttr{});
325325
}
326326

327327
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, mlir::Value dst,
328328
bool isVolatile = false,
329329
mlir::IntegerAttr align = {},
330330
cir::MemOrderAttr order = {}) {
331-
return cir::StoreOp::create(*this, loc, val, dst, isVolatile, align, order);
331+
return cir::StoreOp::create(*this, loc, val, dst, isVolatile, align, order, /*tbaa=*/mlir::ArrayAttr{});
332332
}
333333

334334
[[nodiscard]] cir::GlobalOp createGlobal(mlir::ModuleOp mlirModule,
@@ -353,7 +353,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
353353
auto addr = createAlloca(loc, getPointerTo(type), type, {}, alignmentAttr);
354354
return cir::LoadOp::create(*this, loc, addr, /*isDeref=*/false,
355355
/*isVolatile=*/false, alignmentAttr,
356-
/*mem_order=*/{});
356+
/*mem_order=*/{}, /*tbaa=*/mlir::ArrayAttr{});
357357
}
358358

359359
cir::PtrStrideOp createPtrStride(mlir::Location loc, mlir::Value base,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,14 @@ def CIR_ConstPtrAttr : CIR_Attr<"ConstPtr", "ptr", [TypedAttrInterface]> {
447447
}];
448448
}
449449

450+
//===----------------------------------------------------------------------===//
451+
// TBAAAttr
452+
//===----------------------------------------------------------------------===//
453+
454+
def CIR_TBAAAttr : CIR_Attr<"TBAA", "tbaa", []> {
455+
let summary = "CIR dialect TBAA base attribute";
456+
}
457+
450458
//===----------------------------------------------------------------------===//
451459
// GlobalViewAttr
452460
//===----------------------------------------------------------------------===//

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,10 @@ def CIR_LoadOp : CIR_Op<"load", [
586586
UnitAttr:$isDeref,
587587
UnitAttr:$is_volatile,
588588
OptionalAttr<I64Attr>:$alignment,
589-
OptionalAttr<CIR_MemOrder>:$mem_order);
589+
OptionalAttr<CIR_MemOrder>:$mem_order,
590+
OptionalAttr<ArrayAttr>:$tbaa
591+
);
592+
590593
let results = (outs CIR_AnyType:$result);
591594

592595
let assemblyFormat = [{
@@ -595,6 +598,7 @@ def CIR_LoadOp : CIR_Op<"load", [
595598
(`align` `(` $alignment^ `)`)?
596599
(`atomic` `(` $mem_order^ `)`)?
597600
$addr `:` qualified(type($addr)) `,` type($result) attr-dict
601+
(`tbaa` `(` $tbaa^ `)`)?
598602
}];
599603

600604
// FIXME: add verifier.
@@ -638,13 +642,16 @@ def CIR_StoreOp : CIR_Op<"store", [
638642
[MemWrite]>:$addr,
639643
UnitAttr:$is_volatile,
640644
OptionalAttr<I64Attr>:$alignment,
641-
OptionalAttr<CIR_MemOrder>:$mem_order);
645+
OptionalAttr<CIR_MemOrder>:$mem_order,
646+
OptionalAttr<ArrayAttr>:$tbaa
647+
);
642648

643649
let assemblyFormat = [{
644650
(`volatile` $is_volatile^)?
645651
(`align` `(` $alignment^ `)`)?
646652
(`atomic` `(` $mem_order^ `)`)?
647653
$value `,` $addr attr-dict `:` type($value) `,` qualified(type($addr))
654+
(`tbaa` `(` $tbaa^ `)`)?
648655
}];
649656

650657
// FIXME: add verifier.
@@ -2979,12 +2986,16 @@ def CIR_CopyOp : CIR_Op<"copy",[
29792986
let arguments = (ins
29802987
Arg<CIR_PointerType, "", [MemWrite]>:$dst,
29812988
Arg<CIR_PointerType, "", [MemRead]>:$src,
2982-
UnitAttr:$is_volatile
2989+
UnitAttr:$is_volatile,
2990+
OptionalAttr<ArrayAttr>:$tbaa
29832991
);
29842992

2985-
let assemblyFormat = [{$src `to` $dst (`volatile` $is_volatile^)?
2986-
attr-dict `:` qualified(type($dst))
2993+
let assemblyFormat = [{
2994+
$src `to` $dst (`volatile` $is_volatile^)?
2995+
attr-dict `:` qualified(type($dst))
2996+
(`tbaa` `(` $tbaa^ `)`)?
29872997
}];
2998+
29882999
let hasVerifier = 1;
29893000

29903001
let extraClassDeclaration = [{
@@ -2994,7 +3005,7 @@ def CIR_CopyOp : CIR_Op<"copy",[
29943005
/// Returns the number of bytes to be copied.
29953006
unsigned getLength(const mlir::DataLayout &dt) {
29963007
return dt.getTypeSize(getType().getPointee());
2997-
}
3008+
}
29983009
}];
29993010
}
30003011

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
462462
mlir::IntegerAttr align = getAlignmentAttr(addr.getAlignment());
463463
return cir::LoadOp::create(*this, loc, addr.getPointer(), /*isDeref=*/false,
464464
isVolatile, /*alignment=*/align,
465-
/*mem_order=*/cir::MemOrderAttr{});
465+
/*mem_order=*/cir::MemOrderAttr{}, /*tbaa=*/mlir::ArrayAttr{});
466466
}
467467

468468
cir::LoadOp createAlignedLoad(mlir::Location loc, mlir::Type ty,
@@ -473,7 +473,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
473473
mlir::IntegerAttr alignAttr = getAlignmentAttr(alignment);
474474
return cir::LoadOp::create(*this, loc, ptr, /*isDeref=*/false,
475475
/*isVolatile=*/false, alignAttr,
476-
/*mem_order=*/cir::MemOrderAttr{});
476+
/*mem_order=*/cir::MemOrderAttr{}, /*tbaa=*/mlir::ArrayAttr{});
477477
}
478478

479479
cir::LoadOp

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "CIRGenConstantEmitter.h"
1515
#include "CIRGenFunction.h"
1616
#include "CIRGenModule.h"
17+
#include "CIRGenTBAA.h"
1718
#include "CIRGenValue.h"
1819
#include "mlir/IR/BuiltinAttributes.h"
1920
#include "mlir/IR/Value.h"
@@ -426,6 +427,8 @@ Address CIRGenFunction::getAddrOfBitFieldStorage(LValue base,
426427
LValue CIRGenFunction::emitLValueForBitField(LValue base,
427428
const FieldDecl *field) {
428429
LValueBaseInfo baseInfo = base.getBaseInfo();
430+
TBAAAccessInfo tbaaInfo{};
431+
429432
const CIRGenRecordLayout &layout =
430433
cgm.getTypes().getCIRGenRecordLayout(field->getParent());
431434
const CIRGenBitFieldInfo &info = layout.getBitFieldInfo(field);
@@ -444,7 +447,7 @@ LValue CIRGenFunction::emitLValueForBitField(LValue base,
444447
// TODO(cir): Support TBAA for bit fields.
445448
assert(!cir::MissingFeatures::opTBAA());
446449
LValueBaseInfo fieldBaseInfo(baseInfo.getAlignmentSource());
447-
return LValue::makeBitfield(addr, info, fieldType, fieldBaseInfo);
450+
return LValue::makeBitfield(addr, info, fieldType, fieldBaseInfo, tbaaInfo);
448451
}
449452

450453
LValue CIRGenFunction::emitLValueForField(LValue base, const FieldDecl *field) {

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "CIRGenCXXABI.h"
1616
#include "CIRGenCall.h"
17+
#include "CIRGenTBAA.h"
1718
#include "CIRGenValue.h"
1819
#include "mlir/IR/Location.h"
1920
#include "clang/AST/ExprCXX.h"

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "CIRGenBuilder.h"
1717
#include "CIRGenCall.h"
1818
#include "CIRGenModule.h"
19+
#include "CIRGenTBAA.h"
1920
#include "CIRGenTypeCache.h"
2021
#include "CIRGenValue.h"
2122
#include "EHScopeStack.h"
@@ -838,11 +839,13 @@ class CIRGenFunction : public CIRGenTypeCache {
838839

839840
LValue makeAddrLValue(Address addr, QualType ty,
840841
AlignmentSource source = AlignmentSource::Type) {
841-
return makeAddrLValue(addr, ty, LValueBaseInfo(source));
842+
return makeAddrLValue(addr, ty, LValueBaseInfo(source),
843+
cgm.getTBAAAccessInfo(ty));
842844
}
843845

844-
LValue makeAddrLValue(Address addr, QualType ty, LValueBaseInfo baseInfo) {
845-
return LValue::makeAddr(addr, ty, baseInfo);
846+
LValue makeAddrLValue(Address addr, QualType ty, LValueBaseInfo baseInfo,
847+
TBAAAccessInfo tbaaInfo = {}) {
848+
return LValue::makeAddr(addr, ty, baseInfo, tbaaInfo);
846849
}
847850

848851
void initializeVTablePointers(mlir::Location loc,

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "clang/AST/GlobalDecl.h"
2222
#include "clang/AST/RecordLayout.h"
2323
#include "clang/Basic/SourceManager.h"
24+
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
2425
#include "clang/CIR/Dialect/IR/CIRDialect.h"
2526
#include "clang/CIR/Interfaces/CIROpInterfaces.h"
2627
#include "clang/CIR/MissingFeatures.h"
@@ -2492,3 +2493,72 @@ DiagnosticBuilder CIRGenModule::errorNYI(SourceRange loc,
24922493
llvm::StringRef feature) {
24932494
return errorNYI(loc.getBegin(), feature) << loc;
24942495
}
2496+
2497+
cir::TBAAAttr CIRGenModule::getTBAATypeInfo(QualType QTy) {
2498+
if (!tbaa) {
2499+
return nullptr;
2500+
}
2501+
return tbaa->getTypeInfo(QTy);
2502+
}
2503+
2504+
TBAAAccessInfo CIRGenModule::getTBAAAccessInfo(QualType accessType) {
2505+
if (!tbaa) {
2506+
return TBAAAccessInfo();
2507+
}
2508+
if (getLangOpts().CUDAIsDevice) {
2509+
llvm_unreachable("NYI");
2510+
}
2511+
return tbaa->getAccessInfo(accessType);
2512+
}
2513+
2514+
TBAAAccessInfo
2515+
CIRGenModule::getTBAAVTablePtrAccessInfo(mlir::Type VTablePtrType) {
2516+
if (!tbaa)
2517+
return TBAAAccessInfo();
2518+
llvm_unreachable("NYI");
2519+
return tbaa->getVTablePtrAccessInfo(VTablePtrType);
2520+
}
2521+
2522+
mlir::ArrayAttr CIRGenModule::getTBAAStructInfo(QualType QTy) {
2523+
if (!tbaa)
2524+
return nullptr;
2525+
return tbaa->getTBAAStructInfo(QTy);
2526+
}
2527+
2528+
cir::TBAAAttr CIRGenModule::getTBAABaseTypeInfo(QualType QTy) {
2529+
if (!tbaa) {
2530+
return nullptr;
2531+
}
2532+
return tbaa->getBaseTypeInfo(QTy);
2533+
}
2534+
2535+
cir::TBAAAttr CIRGenModule::getTBAAAccessTagInfo(TBAAAccessInfo tbaaInfo) {
2536+
if (!tbaa) {
2537+
return nullptr;
2538+
}
2539+
return tbaa->getAccessTagInfo(tbaaInfo);
2540+
}
2541+
2542+
TBAAAccessInfo CIRGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
2543+
TBAAAccessInfo TargetInfo) {
2544+
if (!tbaa)
2545+
return TBAAAccessInfo();
2546+
return tbaa->mergeTBAAInfoForCast(SourceInfo, TargetInfo);
2547+
}
2548+
2549+
TBAAAccessInfo
2550+
CIRGenModule::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
2551+
TBAAAccessInfo InfoB) {
2552+
if (!tbaa)
2553+
return TBAAAccessInfo();
2554+
return tbaa->mergeTBAAInfoForConditionalOperator(InfoA, InfoB);
2555+
}
2556+
2557+
TBAAAccessInfo
2558+
CIRGenModule::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
2559+
TBAAAccessInfo SrcInfo) {
2560+
if (!tbaa)
2561+
return TBAAAccessInfo();
2562+
return tbaa->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo);
2563+
}
2564+

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515

1616
#include "CIRGenBuilder.h"
1717
#include "CIRGenCall.h"
18+
#include "CIRGenTBAA.h"
1819
#include "CIRGenTypeCache.h"
1920
#include "CIRGenTypes.h"
2021
#include "CIRGenVTables.h"
2122
#include "CIRGenValue.h"
2223

2324
#include "clang/AST/CharUnits.h"
25+
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
2426
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
2527
#include "clang/CIR/Dialect/IR/CIRDialect.h"
2628

2729
#include "TargetInfo.h"
30+
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
2831
#include "mlir/IR/Builders.h"
2932
#include "mlir/IR/BuiltinOps.h"
3033
#include "mlir/IR/MLIRContext.h"
@@ -85,6 +88,8 @@ class CIRGenModule : public CIRGenTypeCache {
8588

8689
std::unique_ptr<CIRGenCXXABI> abi;
8790

91+
std::unique_ptr<CIRGenTBAA> tbaa;
92+
8893
CIRGenTypes genTypes;
8994

9095
/// Holds information about C++ vtables.
@@ -324,6 +329,51 @@ class CIRGenModule : public CIRGenTypeCache {
324329
/// with codegen.
325330
clang::CharUnits getNaturalTypeAlignment(clang::QualType t,
326331
LValueBaseInfo *baseInfo);
332+
/// Get attribute used to describe accesses to objects of
333+
/// the given type.
334+
cir::TBAAAttr getTBAATypeInfo(QualType QTy);
335+
336+
/// Get TBAA information that describes an access to an object of the given
337+
/// type.
338+
TBAAAccessInfo getTBAAAccessInfo(QualType accessType);
339+
340+
/// Get the TBAA information that describes an access to a virtual table
341+
/// pointer.
342+
TBAAAccessInfo getTBAAVTablePtrAccessInfo(mlir::Type VTablePtrType);
343+
344+
mlir::ArrayAttr getTBAAStructInfo(QualType QTy);
345+
346+
/// Get metadata that describes the given base access type. Return null if the
347+
/// type is not suitable for use in TBAA access tags.
348+
cir::TBAAAttr getTBAABaseTypeInfo(QualType QTy);
349+
350+
cir::TBAAAttr getTBAAAccessTagInfo(TBAAAccessInfo tbaaInfo);
351+
352+
/// Get merged TBAA information for the purposes of type casts.
353+
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
354+
TBAAAccessInfo TargetInfo);
355+
356+
/// Get merged TBAA information for the purposes of conditional operator.
357+
TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
358+
TBAAAccessInfo InfoB);
359+
360+
/// Get merged TBAA information for the purposes of memory transfer calls.
361+
TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
362+
TBAAAccessInfo SrcInfo);
363+
364+
/// Get TBAA information for an access with a given base lvalue.
365+
TBAAAccessInfo getTBAAInfoForSubobject(LValue Base, QualType AccessType) {
366+
if (Base.getTBAAInfo().isMayAlias())
367+
return TBAAAccessInfo::getMayAliasInfo();
368+
return getTBAAAccessInfo(AccessType);
369+
}
370+
371+
template <typename Op>
372+
void decorateOperationWithTBAA(Op op, TBAAAccessInfo tbaaInfo) {
373+
if (auto tag = getTBAAAccessTagInfo(tbaaInfo)) {
374+
op.setTbaaAttr(tag);
375+
}
376+
}
327377

328378
/// TODO: Add TBAAAccessInfo
329379
CharUnits getDynamicOffsetAlignment(CharUnits actualBaseAlign,

0 commit comments

Comments
 (0)