Skip to content

[CIR][CIRGen][TBAA] Add support for struct types #1338

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

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 27 additions & 1 deletion clang/include/clang/CIR/Dialect/IR/CIRTBAAAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,35 @@ def CIR_TBAATagAttr : CIR_Attr<"TBAATag", "tbaa_tag", [], "TBAAAttr"> {
let assemblyFormat = "`<` struct(params) `>`";
}

def CIR_TBAAStructAttr : CIR_Attr<"TBAAStruct",
"tbaa_struct", [], "TBAAAttr"> {
let summary = "Describes a struct type in TBAA";

let parameters = (ins CIR_StructType : $type);

let description = [{
Define a TBAA struct attribute.

Example:
```mlir
// CIR_TBAAStructAttr
!ty_StructA = !cir.struct<struct "StructA" {!u16i, !u32i, !u16i, !u32i} #cir.record.decl.ast>
!ty_StructS = !cir.struct<struct "StructS" {!u16i, !u32i} #cir.record.decl.ast>
#tbaa_struct = #cir.tbaa_struct<type = !ty_StructA>
#tbaa_struct1 = #cir.tbaa_struct<type = !ty_StructS>
```

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

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

def CIR_AnyTBAAAttr : AnyAttrOf<[
CIR_TBAAAttr,
CIR_TBAAOmnipotentChar,
CIR_TBAAScalarAttr,
CIR_TBAAScalarAttr,
CIR_TBAAStructAttr,
CIR_TBAATagAttr
]>;
2 changes: 1 addition & 1 deletion clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct MissingFeatures {
static bool emitTypeCheck() { return false; }
static bool tbaa() { return false; }
static bool tbaaStruct() { return false; }
static bool tbaaTagForStruct() { return false; }
static bool tbaaTagForStruct() { return true; }
static bool tbaaTagForEnum() { return false; }
static bool tbaaTagForBitInt() { return false; }
static bool tbaaVTablePtr() { return false; }
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenTBAA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ cir::TBAAAttr CIRGenTBAA::getTypeInfo(clang::QualType qty) {
// be considered may-alias too.
// function.
if (isValidBaseType(qty)) {
assert(!cir::MissingFeatures::tbaaTagForStruct());
return tbaa_NYI(mlirContext);
assert(cir::MissingFeatures::tbaaTagForStruct());
return cir::TBAAStructAttr::get(mlirContext, types.convertType(qty));
}

const clang::Type *ty = astContext.getCanonicalType(qty).getTypePtr();
Expand Down Expand Up @@ -248,7 +248,7 @@ mlir::ArrayAttr CIRGenTBAA::getTBAAStructInfo(clang::QualType qty) {
}

cir::TBAAAttr CIRGenTBAA::getBaseTypeInfo(clang::QualType qty) {
return tbaa_NYI(mlirContext);
return cir::TBAAStructAttr::get(mlirContext, types.convertType(qty));
}

cir::TBAAAttr CIRGenTBAA::getAccessTagInfo(TBAAAccessInfo tbaaInfo) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct CIROpAsmDialectInterface : public OpAsmDialectInterface {
}
return TypeSwitch<Attribute, AliasResult>(attr)
.Case<cir::TBAAAttr, cir::TBAAOmnipotentCharAttr, cir::TBAAScalarAttr,
cir::TBAATagAttr>([&](auto attr) {
cir::TBAAStructAttr, cir::TBAATagAttr>([&](auto attr) {
os << decltype(attr)::getMnemonic();
return AliasResult::OverridableAlias;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"
#include "clang/CIR/Target/AArch64.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -240,6 +241,12 @@ createLowerModule(mlir::ModuleOp module, mlir::PatternRewriter &rewriter) {
// Create context.
cir_cconv_assert(!cir::MissingFeatures::langOpts());
clang::LangOptions langOpts;
if (auto langAttr = mlir::cast_if_present<cir::LangAttr>(
module->getAttr(cir::CIRDialect::getLangAttrName()))) {
if (langAttr.isCXX()) {
langOpts.CPlusPlus = true;
}
}

// FIXME(cir): This just uses the default code generation options. We need to
// account for custom options.
Expand Down
Loading