Skip to content

Commit 5ea324a

Browse files
committed
address review comments
1 parent bdf6890 commit 5ea324a

File tree

8 files changed

+298
-290
lines changed

8 files changed

+298
-290
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,19 +1104,20 @@ def BitfieldInfoAttr : CIR_Attr<"BitfieldInfo", "bitfield_info"> {
11041104
//===----------------------------------------------------------------------===//
11051105
// AnnotationAttr
11061106
//===----------------------------------------------------------------------===//
1107+
11071108
def AnnotationAttr : CIR_Attr<"Annotation", "annotation"> {
11081109
let summary = "Annotation attribute for global variables and functions";
11091110
let description = [{
1110-
Represent C/C++ attribute of annotate in CIR. example c code is:
1111+
Represent C/C++ attribute of annotate in CIR. Example c code is:
11111112
```
1112-
in test.c line 10
11131113
int *a __attribute__((annotate("testptr", "21", 12 )));
11141114
```
1115-
this example code, the AnnotationAttr records annotation name "testptr",
1116-
and arguments "21" and 12 as ArrayAttr args for global variable a.
1115+
In this example code, the `AnnotationAttr` records annotation name "testptr",
1116+
and arguments "21" and 12 as `ArrayAttr` type parameter `args`
1117+
for global variable `a`.
11171118
}];
11181119

1119-
// args is empty when there is no arg.
1120+
// The parameter args is empty when there is no arg.
11201121
let parameters = (ins "StringAttr":$name,
11211122
"ArrayAttr":$args);
11221123

@@ -1128,27 +1129,26 @@ def AnnotationAttr : CIR_Attr<"Annotation", "annotation"> {
11281129
}
11291130

11301131
//===----------------------------------------------------------------------===//
1131-
// AnnotationValueAttr
1132+
// GlobalAnnotationValueAttr
11321133
//===----------------------------------------------------------------------===//
1133-
def AnnotationValueAttr : CIR_Attr<"AnnotationValue", "annotation value"> {
1134+
1135+
def GlobalAnnotationValueAttr : CIR_Attr<"GlobalAnnotationValue", "annotation value"> {
11341136
let summary = "An annotation value, consist of name of a global var or func"
11351137
"and one of its annotations";
11361138
let description = [{
11371139
This is element type of annotation value array, which holds the annotation
11381140
values for all global variables and functions in a module.
1139-
This array is used to create the initial value of a global annotation
1141+
This array is used to create the initial value of a global annotation
11401142
metadata variable in LLVM IR.
11411143
}];
11421144

11431145
// Here the name is the name of a global var or func.
1144-
let parameters = (ins "StringAttr":$name,
1145-
"AnnotationAttr":$value);
1146+
let parameters = (ins "ArrayAttr":$value);
11461147

1148+
let assemblyFormat = " $value ";
11471149

1148-
let assemblyFormat = "`<` struct($name, $value) `>`";
1149-
11501150
// Enable verifier.
1151-
let genVerifyDecl = 1;
1151+
let genVerifyDecl = 1;
11521152
}
11531153

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@ def GlobalLinkageKind : I32EnumAttr<
21712171
"Linkage type/kind",
21722172
[Global_ExternalLinkage, Global_AvailableExternallyLinkage,
21732173
Global_LinkOnceAnyLinkage, Global_LinkOnceODRLinkage,
2174-
Global_WeakAnyLinkage, Global_WeakODRLinkage,
2174+
Global_WeakAnyLinkage, Global_WeakODRLinkage,
21752175
Global_InternalLinkage, Global_PrivateLinkage,
21762176
Global_ExternalWeakLinkage, Global_CommonLinkage
21772177
]> {

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3190,38 +3190,29 @@ LangAS CIRGenModule::getGlobalVarAddressSpace(const VarDecl *D) {
31903190
return getTargetCIRGenInfo().getGlobalVarAddressSpace(*this, D);
31913191
}
31923192

3193-
mlir::StringAttr CIRGenModule::buildAnnotationString(StringRef Str) {
3194-
auto &Astr = AnnotationStrings[Str];
3195-
if (Astr)
3196-
return Astr;
3197-
Astr = builder.getStringAttr(Str);
3198-
return Astr;
3199-
}
3200-
3201-
mlir::ArrayAttr CIRGenModule::buildAnnotationArgs(const AnnotateAttr *Attr) {
3202-
ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()};
3203-
if (Exprs.empty()) {
3193+
mlir::ArrayAttr CIRGenModule::buildAnnotationArgs(const AnnotateAttr *attr) {
3194+
ArrayRef<Expr *> exprs = {attr->args_begin(), attr->args_size()};
3195+
if (exprs.empty()) {
32043196
return mlir::ArrayAttr::get(builder.getContext(), {});
32053197
}
3206-
llvm::FoldingSetNodeID ID;
3207-
for (Expr *E : Exprs) {
3208-
ID.Add(cast<clang::ConstantExpr>(E)->getAPValueResult());
3198+
llvm::FoldingSetNodeID id;
3199+
for (Expr *e : exprs) {
3200+
id.Add(cast<clang::ConstantExpr>(e)->getAPValueResult());
32093201
}
3210-
auto &Lookup = AnnotationArgs[ID.ComputeHash()];
3211-
if (Lookup)
3212-
return Lookup;
3202+
mlir::ArrayAttr &lookup = AnnotationArgs[id.ComputeHash()];
3203+
if (lookup)
3204+
return lookup;
32133205

3214-
llvm::SmallVector<mlir::Attribute, 4> Args;
3215-
Args.reserve(Exprs.size());
3216-
for (Expr *E : Exprs) {
3206+
llvm::SmallVector<mlir::Attribute, 4> args;
3207+
args.reserve(exprs.size());
3208+
for (Expr *e : exprs) {
32173209
if (const auto StrE =
3218-
::clang::dyn_cast<clang::StringLiteral>(E->IgnoreParenCasts())) {
3210+
::clang::dyn_cast<clang::StringLiteral>(e->IgnoreParenCasts())) {
32193211
// Add trailing null character as StringLiteral->getString() does not
3220-
Args.push_back(
3221-
buildAnnotationString(std::string(StrE->getString()) + '\0'));
3212+
args.push_back(builder.getStringAttr(StrE->getString()));
32223213
} else if (const auto IntE = ::clang::dyn_cast<clang::IntegerLiteral>(
3223-
E->IgnoreParenCasts())) {
3224-
Args.push_back(mlir::IntegerAttr::get(
3214+
e->IgnoreParenCasts())) {
3215+
args.push_back(mlir::IntegerAttr::get(
32253216
mlir::IntegerType::get(builder.getContext(),
32263217
IntE->getValue().getBitWidth()),
32273218
IntE->getValue()));
@@ -3230,32 +3221,30 @@ mlir::ArrayAttr CIRGenModule::buildAnnotationArgs(const AnnotateAttr *Attr) {
32303221
}
32313222
}
32323223
// Create const struct attr for these arguments
3233-
mlir::ArrayAttr ArgsArrayAttr = builder.getArrayAttr(Args);
3234-
Lookup = ArgsArrayAttr;
3235-
return Lookup;
3224+
mlir::ArrayAttr argsArrayAttr = builder.getArrayAttr(args);
3225+
lookup = argsArrayAttr;
3226+
return lookup;
32363227
}
32373228

32383229
mlir::cir::AnnotationAttr
3239-
CIRGenModule::buildAnnotateAttr(mlir::Operation *GV, const AnnotateAttr *AA,
3240-
SourceLocation L) {
3241-
3242-
auto AnnoGV = buildAnnotationString(std::string(AA->getAnnotation()) + '\0');
3243-
auto Args = buildAnnotationArgs(AA);
3244-
return mlir::cir::AnnotationAttr::get(builder.getContext(), AnnoGV, Args);
3230+
CIRGenModule::buildAnnotateAttr(const AnnotateAttr *aa) {
3231+
auto annoGV = builder.getStringAttr(aa->getAnnotation());
3232+
mlir::ArrayAttr args = buildAnnotationArgs(aa);
3233+
return mlir::cir::AnnotationAttr::get(builder.getContext(), annoGV, args);
32453234
}
32463235

3247-
void CIRGenModule::addGlobalAnnotations(const ValueDecl *D,
3248-
mlir::Operation *GV) {
3249-
assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
3250-
assert((isa<GlobalOp>(GV) || isa<FuncOp>(GV)) &&
3236+
void CIRGenModule::addGlobalAnnotations(const ValueDecl *d,
3237+
mlir::Operation *gv) {
3238+
assert(d->hasAttr<AnnotateAttr>() && "no annotate attribute");
3239+
assert((isa<GlobalOp>(gv) || isa<FuncOp>(gv)) &&
32513240
"annotation only on globals");
3252-
llvm::SmallVector<mlir::Attribute, 4> Annotations;
3253-
for (const auto *I : D->specific_attrs<AnnotateAttr>())
3254-
Annotations.push_back(buildAnnotateAttr(GV, I, D->getLocation()));
3255-
if (auto global = dyn_cast<mlir::cir::GlobalOp>(GV))
3256-
global.setAnnotationsAttr(builder.getArrayAttr(Annotations));
3257-
else if (auto func = dyn_cast<mlir::cir::FuncOp>(GV))
3258-
func.setAnnotationsAttr(builder.getArrayAttr(Annotations));
3241+
llvm::SmallVector<mlir::Attribute, 4> annotations;
3242+
for (const auto *i : d->specific_attrs<AnnotateAttr>())
3243+
annotations.push_back(buildAnnotateAttr(i));
3244+
if (auto global = dyn_cast<mlir::cir::GlobalOp>(gv))
3245+
global.setAnnotationsAttr(builder.getArrayAttr(annotations));
3246+
else if (auto func = dyn_cast<mlir::cir::FuncOp>(gv))
3247+
func.setAnnotationsAttr(builder.getArrayAttr(annotations));
32593248
}
32603249

32613250
void CIRGenModule::buildGlobalAnnotations() {

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -783,24 +783,18 @@ class CIRGenModule : public CIRGenTypeCache {
783783
/// functions, because global variables need to deffred emission.
784784
void buildGlobalAnnotations();
785785

786-
/// Emit an annotation string as the returned StringAttr is needed to
787-
/// assemble AnnotationAttr for a GlobalOp or FuncOp.
788-
mlir::StringAttr buildAnnotationString(StringRef Str);
789-
790786
/// Emit additional args of the annotation.
791-
mlir::ArrayAttr buildAnnotationArgs(const AnnotateAttr *Attr);
787+
mlir::ArrayAttr buildAnnotationArgs(const AnnotateAttr *attr);
792788

793789
/// Create cir::AnnotationAttr which contains the annotation
794790
/// information for a given GlobalValue. Notice that a GlobalValue could
795791
/// have multiple annotations, and this function creates attribute for
796792
/// one of them.
797-
mlir::cir::AnnotationAttr buildAnnotateAttr(mlir::Operation *GV,
798-
const AnnotateAttr *AA,
799-
SourceLocation L);
793+
mlir::cir::AnnotationAttr buildAnnotateAttr(const AnnotateAttr *aa);
800794

801795
/// Add global annotations that are set on D, for the global GV. Those
802796
/// annotations are emitted during lowering to the LLVM code.
803-
void addGlobalAnnotations(const ValueDecl *D, mlir::Operation *GV);
797+
void addGlobalAnnotations(const ValueDecl *d, mlir::Operation *gv);
804798
};
805799
} // namespace cir
806800

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -545,13 +545,24 @@ void MethodAttr::print(AsmPrinter &printer) const {
545545
}
546546

547547
//===----------------------------------------------------------------------===//
548-
// AnnotationValueAttr definitions
548+
// GlobalAnnotationValueAttr definitions
549549
//===----------------------------------------------------------------------===//
550-
LogicalResult AnnotationValueAttr::verify(
551-
function_ref<::mlir::InFlightDiagnostic()> emitError, mlir::StringAttr name,
552-
mlir::cir::AnnotationAttr value) {
553-
if (name.empty()) {
554-
emitError() << "annotation has to be associated with a global variable";
550+
LogicalResult GlobalAnnotationValueAttr::verify(
551+
function_ref<::mlir::InFlightDiagnostic()> emitError,
552+
mlir::ArrayAttr value) {
553+
if (value.size() < 2) {
554+
emitError()
555+
<< "GlobalAnnotationValueAttr should at least have two elements";
556+
return failure();
557+
} else if (!::mlir::isa<mlir::StringAttr>(value[0])) {
558+
emitError()
559+
<< "The first element of GlobalAnnotationValueAttr must be string";
560+
return failure();
561+
}
562+
auto annoPart = ::mlir::cast<mlir::cir::AnnotationAttr>(value[1]);
563+
if (!annoPart) {
564+
emitError() << "The second element of GlobalAnnotationValueAttr must be "
565+
"AnnotationValueAttr";
555566
return failure();
556567
}
557568
return success();

clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
156156
/// as we need view to annotated global values to create global annotation var
157157
/// later in LLVM lowering.
158158
// One global value could have multiple AnnotateAttr
159-
std::vector<std::pair<mlir::Attribute, mlir::Operation *>> globalAnnotations;
159+
SmallVector<std::pair<mlir::Attribute, mlir::Operation *>> globalAnnotations;
160160
};
161161
} // namespace
162162

@@ -1080,15 +1080,19 @@ void LoweringPreparePass::lowerIterEndOp(IterEndOp op) {
10801080
void LoweringPreparePass::buildGlobalAnnotationValues() {
10811081
if (globalAnnotations.empty())
10821082
return;
1083-
std::vector<mlir::Attribute> annotationValueVec;
1083+
SmallVector<mlir::Attribute> annotationValueVec;
1084+
annotationValueVec.reserve(globalAnnotations.size());
10841085

10851086
for (auto &annotEntry : globalAnnotations) {
10861087
auto annot = dyn_cast<mlir::cir::AnnotationAttr>(annotEntry.first);
1087-
auto op = annotEntry.second;
1088+
mlir::Operation *op = annotEntry.second;
10881089
auto globalValue = dyn_cast<mlir::SymbolOpInterface>(op);
1089-
auto globalValueName = globalValue.getNameAttr();
1090-
auto valueEntry = mlir::cir::AnnotationValueAttr::get(
1091-
theModule.getContext(), globalValueName, annot);
1090+
mlir::StringAttr globalValueName = globalValue.getNameAttr();
1091+
SmallVector<mlir::Attribute, 2> entryArray = {globalValueName, annot};
1092+
mlir::cir::GlobalAnnotationValueAttr valueEntry =
1093+
mlir::cir::GlobalAnnotationValueAttr::get(
1094+
theModule.getContext(),
1095+
mlir::ArrayAttr::get(theModule.getContext(), entryArray));
10921096
annotationValueVec.push_back(valueEntry);
10931097
}
10941098
mlir::ArrayAttr annotationValueArray =

0 commit comments

Comments
 (0)