Skip to content

Commit c25e08c

Browse files
authored
[SYCL-MLIR]: FunctionType and Attributes Infrastructure (#7126)
In order to abide by the ABI calling conventions clang uses an exhaustive infrastructure. In cgeist we take a number of shortcuts. This is problematic because: - the cgeist implementation to add attributes to function types, parameters, call arguments is incomplete - the current way cgeist computes function attributes does not take into account the ABI constraints This PR enhances the way cgeist attaches function attributes to a function definition. Signed-off-by: Tiotto, Ettore <[email protected]>
1 parent a305edf commit c25e08c

File tree

13 files changed

+1466
-292
lines changed

13 files changed

+1466
-292
lines changed

polygeist/tools/cgeist/Lib/Attributes.cc

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,29 @@ static void addToPassThroughAttr(NamedAttribute &PassThroughAttr,
4040
std::vector<mlir::Attribute> Vec =
4141
PassThroughAttr.getValue().cast<ArrayAttr>().getValue().vec();
4242

43-
// TODO: find a way to add the attributes only if one does not exist already,
44-
// and keep the list in sorted order.
43+
// TODO: find a way to add the attributes only if one does not exist already.
4544
if (Attr.getValue().isa<UnitAttr>())
4645
Vec.push_back(Attr.getName());
4746
else
4847
Vec.push_back(ArrayAttr::get(&Ctx, {Attr.getName(), Attr.getValue()}));
4948

49+
auto Comp = [&](const mlir::Attribute &A1, const mlir::Attribute &A2) {
50+
assert(A1.isa<StringAttr>() || A1.isa<ArrayAttr>());
51+
assert(A2.isa<StringAttr>() || A2.isa<ArrayAttr>());
52+
53+
if (auto StrA1 = A1.dyn_cast<StringAttr>()) {
54+
if (auto StrA2 = A2.dyn_cast<StringAttr>())
55+
return StrA1 < StrA2;
56+
return true;
57+
}
58+
59+
auto ArrA1 = A1.cast<ArrayAttr>();
60+
if (auto ArrA2 = A2.dyn_cast<ArrayAttr>())
61+
return ArrA1[0].cast<StringAttr>() < ArrA2[0].cast<StringAttr>();
62+
return false;
63+
};
64+
65+
llvm::sort(Vec.begin(), Vec.end(), Comp);
5066
PassThroughAttr.setValue(ArrayAttr::get(&Ctx, Vec));
5167

5268
LLVM_DEBUG({
@@ -81,26 +97,36 @@ static void addToPassThroughAttr(mlir::NamedAttribute &PassThroughAttr,
8197
// AttributeList Method Implementations
8298
//===----------------------------------------------------------------------===//
8399

100+
AttributeList::AttributeList(const mlir::NamedAttrList &FnAttrs,
101+
const mlir::NamedAttrList &RetAttrs,
102+
llvm::ArrayRef<mlir::NamedAttrList> ParamAttrs)
103+
: FnAttrs(FnAttrs), RetAttrs(RetAttrs), ParamAttrs(ParamAttrs) {}
104+
84105
AttributeList &
85106
AttributeList::addAttrs(const AttrBuilder &FnAttrB, const AttrBuilder &RetAttrB,
86107
llvm::ArrayRef<mlir::NamedAttrList> Attrs) {
87108
return addFnAttrs(FnAttrB).addRetAttrs(RetAttrB).addParamAttrs(Attrs);
88109
}
89110

90111
AttributeList &AttributeList::addFnAttrs(const AttrBuilder &B) {
91-
for (const NamedAttribute &NewNamedAttr : B.getAttrs()) {
92-
Optional<NamedAttribute> ExistingAttr =
93-
FnAttrs.getNamed(NewNamedAttr.getName());
94-
if (!ExistingAttr) {
95-
FnAttrs.append(NewNamedAttr);
112+
return addFnAttrs(B.getAttrs(), B.getContext());
113+
}
114+
115+
AttributeList &AttributeList::addFnAttrs(const NamedAttrList &Attrs,
116+
MLIRContext &Ctx) {
117+
for (const NamedAttribute &NewFnAttr : Attrs) {
118+
Optional<NamedAttribute> ExistingFnAttr =
119+
FnAttrs.getNamed(NewFnAttr.getName());
120+
if (!ExistingFnAttr) {
121+
FnAttrs.append(NewFnAttr);
96122
continue;
97123
}
98124

99125
// Merge the 'passthrough' attribute lists.
100-
if (ExistingAttr->getName() == PassThroughAttrName) {
101-
auto Attrs = NewNamedAttr.getValue().cast<ArrayAttr>();
102-
addToPassThroughAttr(*ExistingAttr, Attrs, B.getContext());
103-
FnAttrs.set(ExistingAttr->getName(), ExistingAttr->getValue());
126+
if (ExistingFnAttr->getName() == PassThroughAttrName) {
127+
auto Attrs = NewFnAttr.getValue().cast<ArrayAttr>();
128+
addToPassThroughAttr(*ExistingFnAttr, Attrs, Ctx);
129+
FnAttrs.set(ExistingFnAttr->getName(), ExistingFnAttr->getValue());
104130
continue;
105131
}
106132

@@ -111,7 +137,12 @@ AttributeList &AttributeList::addFnAttrs(const AttrBuilder &B) {
111137
}
112138

113139
AttributeList &AttributeList::addRetAttrs(const AttrBuilder &B) {
114-
for (const NamedAttribute &NewNamedAttr : B.getAttrs()) {
140+
return addRetAttrs(B.getAttrs(), B.getContext());
141+
}
142+
143+
AttributeList &AttributeList::addRetAttrs(const mlir::NamedAttrList &Attrs,
144+
mlir::MLIRContext &Ctx) {
145+
for (const NamedAttribute &NewNamedAttr : Attrs) {
115146
Optional<NamedAttribute> ExistingAttr =
116147
RetAttrs.getNamed(NewNamedAttr.getName());
117148
if (!ExistingAttr) {

polygeist/tools/cgeist/Lib/Attributes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class AttrBuilder;
2424
class AttributeList {
2525
public:
2626
AttributeList() = default;
27+
AttributeList(const mlir::NamedAttrList &FnAttrs,
28+
const mlir::NamedAttrList &RetAttrs,
29+
llvm::ArrayRef<mlir::NamedAttrList> ParamAttrs);
2730

2831
//===--------------------------------------------------------------------===//
2932
// AttributeList Mutation
@@ -36,9 +39,13 @@ class AttributeList {
3639

3740
/// Add function attributes to the list.
3841
AttributeList &addFnAttrs(const AttrBuilder &B);
42+
AttributeList &addFnAttrs(const mlir::NamedAttrList &Attrs,
43+
mlir::MLIRContext &Ctx);
3944

4045
/// Add return value attributes to the list.
4146
AttributeList &addRetAttrs(const AttrBuilder &B);
47+
AttributeList &addRetAttrs(const mlir::NamedAttrList &Attrs,
48+
mlir::MLIRContext &Ctx);
4249

4350
/// Add parameters attributes to the list.
4451
AttributeList &addParamAttrs(llvm::ArrayRef<mlir::NamedAttrList> Attrs);

0 commit comments

Comments
 (0)