Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][CIRGen][TBAA] Add support for TBAA #1076

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 146 additions & 2 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 @@ -1249,7 +1250,150 @@ def GlobalAnnotationValuesAttr : CIR_Attr<"GlobalAnnotationValues",
let genVerifyDecl = 1;
}


//===----------------------------------------------------------------------===//
// TBAAAttr
//===----------------------------------------------------------------------===//
PikachuHyA marked this conversation as resolved.
Show resolved Hide resolved

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

//===----------------------------------------------------------------------===//
// TBAATypeDescriptorAttr
//===----------------------------------------------------------------------===//

def CIR_TBAAMemberAttr : CIR_Attr<"TBAAMember", "tbaa_member", []> {
let summary = "Attribute representing a member of a TBAA structured type.";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a description and an example. Same for the others


let parameters = (ins "TBAAAttr":$typeDesc,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeDesc -> type_desc, please fix all camelcase on tablegen params and related

"int64_t":$offset,
"int64_t":$size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like offset and size is something that can be obtained by looking at the type.

General question for this PR: why do we need to re-encode the same information CIR already has? The requirement is to lower to identical LLVM TBAA information, that shouldn't mean we have to replicate all of it in CIR - how can we reuse more of what we already have?


let builders = [
AttrBuilderWithInferredContext<(ins "TBAAAttr":$typeDesc,
"int64_t":$offset,
"int64_t":$size), [{
return $_get(typeDesc.getContext(), typeDesc, offset, size);
}]>
];
let assemblyFormat = "`<` params `>`";
}

def CIR_TBAAMemberAttrArray : ArrayRefParameter<"TBAAMemberAttr"> {
let summary = "Array of TBAAMemberAttr attributes.";

let printer = [{
$_printer << '{';
llvm::interleaveComma($_self, $_printer, [&](TBAAMemberAttr attr) {
$_printer.printStrippedAttrOrType(attr);
});
$_printer << '}';
}];

let parser = [{
[&]() -> llvm::FailureOr<llvm::SmallVector<TBAAMemberAttr>> {
using Result = llvm::SmallVector<TBAAMemberAttr>;
if ($_parser.parseLBrace())
return mlir::failure();
llvm::FailureOr<Result> result = mlir::FieldParser<Result>::parse($_parser);
if (failed(result))
return mlir::failure();
if ($_parser.parseRBrace())
return mlir::failure();
return result;
}()
}];
}

class CIR_TBAATypeDescriptorAttr<string name, string attrMnemonic>: CIR_Attr<name, attrMnemonic, [], "TBAAAttr"> {
let summary = "Base class for TBAA type descriptor attributes.";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this base class and what's the intent to compose on top of it? Same for CIR_TBAAAttr, can you elaborate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still waiting for an answer for this since it affects my review of #1220

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for overlooking this question.

I make a distinction between scalar types and struct types. The use of class CIR_TBAATypeDescriptorAttr serves to constrain both CIR_TBAAScalarTypeDescriptorAttr and CIR_TBAAStructTypeDescriptorAttr as TBAA type descriptor attributes. If CIR_TBAATypeDescriptorAttr is redundant, we can remove it.

The purpose of using CIR_TBAAAttr as a base class in C++ is to provide a unified way to handle both CIR_TBAAScalarTypeDescriptorAttr and CIR_TBAAStructTypeDescriptorAttr.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen them get used in the PR though, hence the question. If are actually using it then fine, otherwise best wait until it's used to introduce. Let's move any discussion over the actual patches that might get landed, thanks for the reply!

}

def CIR_TBAAScalarTypeDescriptorAttr : CIR_TBAATypeDescriptorAttr<"TBAAScalarTypeDescriptor",
"tbaa_scalar_type_desc"> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation feels odd here, same for other some other ones

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any tools available to format .td files? For example, just as we can use clang-format to format .cpp files:

clang-format -i /path/to/cpp_file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? Last I followed was https://discourse.llvm.org/t/formating-mlir-tablegen-code/60767, but I'm not sure there has been any development.

let summary = "Describes a scalar type in TBAA with an identifier.";

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

let description = [{
Define a TBAA attribute.

Example:
```mlir
// CIR_TBAAScalarTypeDescriptorAttr
#tbaa_scalar_type_desc = #cir.tbaa_scalar_type_desc<id = "any pointer">
#tbaa_scalar_type_desc1 = #cir.tbaa_scalar_type_desc<id = "int">
```

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

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

def CIR_TBAAStructTypeDescriptorAttr : CIR_TBAATypeDescriptorAttr<"TBAAStructTypeDescriptor",
"tbaa_struct_type_desc"> {
let summary = "Describes a structure type in TBAA with an identifier and member attributes.";

let parameters = (ins StringRefParameter<>:$id, CIR_TBAAMemberAttrArray:$members);

let description = [{
Define a TBAA attribute.

Example:
```mlir
// CIR_TBAAScalarTypeDescriptorAttr
#tbaa_scalar_type_desc = #cir.tbaa_scalar_type_desc<id = "any pointer">
#tbaa_scalar_type_desc1 = #cir.tbaa_scalar_type_desc<id = "int">
// CIR_TBAAStructTypeDescriptorAttr
#tbaa_struct_type_desc = #cir.tbaa_struct_type_desc<id = "StructDemo",
members = {<#tbaa_scalar_type_desc2, 0, 2>,
<#tbaa_scalar_type_desc1, 4, 4>}>
```

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

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

//===----------------------------------------------------------------------===//
// TBAATagAttr
//===----------------------------------------------------------------------===//

def CIR_TBAATagAttr : CIR_Attr<"TBAATag", "tbaa_tag", []> {
let summary = "Tag attribute for TBAA, connecting base type and access type with metadata.";

let parameters = (ins "TBAAAttr":$base_type,
"TBAAAttr":$access_type,
"int64_t":$offset,
"int64_t":$size);

let description = [{
Define a TBAA attribute.

Example:
```mlir
// CIR_TBAAScalarTypeDescriptorAttr
#tbaa_scalar_type_desc = #cir.tbaa_scalar_type_desc<id = "any pointer">
#tbaa_scalar_type_desc1 = #cir.tbaa_scalar_type_desc<id = "int">
// CIR_TBAAStructTypeDescriptorAttr
#tbaa_struct_type_desc = #cir.tbaa_struct_type_desc<id = "StructDemo",
members = {<#tbaa_scalar_type_desc2, 0, 2>,
<#tbaa_scalar_type_desc1, 4, 4>}>
// CIR_TBAATagAttr
#tbaa_tag = #cir.tbaa_tag<base_type = #tbaa_struct_type_desc,
access_type = #tbaa_scalar_type_desc1, offset = 4, size = 4>
```

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

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

include "clang/CIR/Dialect/IR/CIROpenCLAttrs.td"
Expand Down
Loading
Loading