Skip to content

Commit

Permalink
[CIR][CIRGen][TBAA] Add support for scalar types
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuHyA committed Feb 10, 2025
1 parent 53335ae commit a54da03
Show file tree
Hide file tree
Showing 24 changed files with 788 additions and 56 deletions.
6 changes: 3 additions & 3 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
/*alignment=*/intAttr,
/*mem_order=*/
cir::MemOrderAttr{},
/*tbaa=*/mlir::ArrayAttr{});
/*tbaa=*/cir::TBAAAttr{});
}

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

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

cir::MemCpyOp createMemCpy(mlir::Location loc, mlir::Value dst,
Expand Down
8 changes: 4 additions & 4 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ include "clang/CIR/Interfaces/ASTAttrInterfaces.td"
// CIR Attrs
//===----------------------------------------------------------------------===//

class CIR_Attr<string name, string attrMnemonic, list<Trait> traits = []>
: AttrDef<CIR_Dialect, name, traits> {
class CIR_Attr<string name, string attrMnemonic, list<Trait> traits = [],
string baseCppClass = "::mlir::Attribute">
: AttrDef<CIR_Dialect, name, traits, baseCppClass> {
let mnemonic = attrMnemonic;
}

Expand Down Expand Up @@ -1323,8 +1324,7 @@ def GlobalAnnotationValuesAttr : CIR_Attr<"GlobalAnnotationValues",
let genVerifyDecl = 1;
}

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

include "clang/CIR/Dialect/IR/CIROpenCLAttrs.td"

Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ def LoadOp : CIR_Op<"load", [
UnitAttr:$is_volatile,
OptionalAttr<I64Attr>:$alignment,
OptionalAttr<MemOrder>:$mem_order,
OptionalAttr<ArrayAttr>:$tbaa
OptionalAttr<CIR_AnyTBAAAttr>:$tbaa
);
let results = (outs CIR_AnyType:$result);

Expand Down Expand Up @@ -658,7 +658,7 @@ def StoreOp : CIR_Op<"store", [
UnitAttr:$is_volatile,
OptionalAttr<I64Attr>:$alignment,
OptionalAttr<MemOrder>:$mem_order,
OptionalAttr<ArrayAttr>:$tbaa);
OptionalAttr<CIR_AnyTBAAAttr>:$tbaa);

let assemblyFormat = [{
(`volatile` $is_volatile^)?
Expand Down Expand Up @@ -4121,7 +4121,7 @@ def CopyOp : CIR_Op<"copy",
let arguments = (ins Arg<CIR_PointerType, "", [MemWrite]>:$dst,
Arg<CIR_PointerType, "", [MemRead]>:$src,
UnitAttr:$is_volatile,
OptionalAttr<ArrayAttr>:$tbaa);
OptionalAttr<CIR_TBAAAttr>:$tbaa);
let summary = "Copies contents from a CIR pointer to another";
let description = [{
Given two CIR pointers, `src` and `dst`, `cir.copy` will copy the memory
Expand Down
58 changes: 58 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRTBAAAttrs.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===----------------------------------------------------------------------===//
// TBAAAttr
//===----------------------------------------------------------------------===//

def CIR_TBAAAttr : CIR_Attr<"TBAA", "tbaa", []> {
let summary = "CIR dialect TBAA base attribute";
}

//===----------------------------------------------------------------------===//
// TBAAOmnipotentCharAttr
//===----------------------------------------------------------------------===//

def CIR_TBAAOmnipotentChar
: CIR_Attr<"TBAAOmnipotentChar", "tbaa_omnipotent_char", [], "TBAAAttr"> {
let summary = "Describes a special scalar type, the omnipotent char type.";
}

//===----------------------------------------------------------------------===//
// TBAAScalarAttr
//===----------------------------------------------------------------------===//

def CIR_TBAAScalarAttr : CIR_Attr<"TBAAScalar", "tbaa_scalar", [], "TBAAAttr"> {
let summary = "Describes a scalar type in TBAA with an identifier.";

let parameters = (ins StringRefParameter<> : $id, CIR_AnyType : $type);

let description = [{
Define a TBAA scalar attribute.

Example:
```mlir
// CIR_TBAAScalarAttr
#tbaa_scalar = #cir.tbaa_scalar<id = "int", type = !s32i>
#tbaa_scalar1 = #cir.tbaa_scalar<id = "long long", type = !s64i>
```

See the following link for more details:
https://llvm.org/docs/LangRef.html#tbaa-metadata
}];

let assemblyFormat = "`<` struct(params) `>`";
}

def CIR_TBAATagAttr : CIR_Attr<"TBAATag", "tbaa_tag", [], "TBAAAttr"> {
let parameters = (ins CIR_TBAAAttr
: $base, CIR_TBAAAttr
: $access, "int64_t"
: $offset);

let assemblyFormat = "`<` struct(params) `>`";
}

def CIR_AnyTBAAAttr : AnyAttrOf<[
CIR_TBAAAttr,
CIR_TBAAOmnipotentChar,
CIR_TBAAScalarAttr,
CIR_TBAATagAttr
]>;
11 changes: 10 additions & 1 deletion clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,16 @@ struct MissingFeatures {
// sanitizer related type check features
static bool emitTypeCheck() { return false; }
static bool tbaa() { return false; }
static bool tbaa_struct() { return false; }
static bool tbaaStruct() { return false; }
static bool tbaaTagForStruct() { return false; }
static bool tbaaTagForEnum() { return false; }
static bool tbaaTagForBitInt() { return false; }
static bool tbaaVTablePtr() { return false; }
static bool tbaaIncompleteType() { return false; }
static bool tbaaMergeTBAAInfo() { return false; }
static bool tbaaMayAlias() { return false; }
static bool tbaaNewStructPath() { return false; }
static bool tbaaPointer() { return false; }
static bool emitNullabilityCheck() { return false; }
static bool ptrAuth() { return false; }

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
return create<cir::LoadOp>(
loc, addr.getElementType(), addr.getPointer(), /*isDeref=*/false,
/*is_volatile=*/isVolatile, /*alignment=*/mlir::IntegerAttr{},
/*mem_order=*/cir::MemOrderAttr{}, /*tbaa=*/mlir::ArrayAttr{});
/*mem_order=*/cir::MemOrderAttr{}, /*tbaa=*/cir::TBAAAttr{});
}

mlir::Value createAlignedLoad(mlir::Location loc, mlir::Type ty,
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,7 @@ void CIRGenFunction::emitAggregateCopy(LValue Dest, LValue Src, QualType Ty,
// Determine the metadata to describe the position of any padding in this
// memcpy, as well as the TBAA tags for the members of the struct, in case
// the optimizer wishes to expand it in to scalar memory operations.
assert(!cir::MissingFeatures::tbaa_struct() && "tbaa.struct NYI");
assert(!cir::MissingFeatures::tbaaStruct() && "tbaa.struct NYI");
if (CGM.getCodeGenOpts().NewStructPathTBAA) {
TBAAAccessInfo TBAAInfo = CGM.mergeTBAAInfoForMemoryTransfer(
Dest.getTBAAInfo(), Src.getTBAAInfo());
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4098,7 +4098,7 @@ cir::TBAAAttr CIRGenModule::getTBAABaseTypeInfo(QualType QTy) {
return tbaa->getBaseTypeInfo(QTy);
}

mlir::ArrayAttr CIRGenModule::getTBAAAccessTagInfo(TBAAAccessInfo tbaaInfo) {
cir::TBAAAttr CIRGenModule::getTBAAAccessTagInfo(TBAAAccessInfo tbaaInfo) {
if (!tbaa) {
return nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ class CIRGenModule : public CIRGenTypeCache {
/// type is not suitable for use in TBAA access tags.
cir::TBAAAttr getTBAABaseTypeInfo(QualType QTy);

mlir::ArrayAttr getTBAAAccessTagInfo(TBAAAccessInfo tbaaInfo);
cir::TBAAAttr getTBAAAccessTagInfo(TBAAAccessInfo tbaaInfo);

/// Get merged TBAA information for the purposes of type casts.
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
Expand Down
Loading

0 comments on commit a54da03

Please sign in to comment.