Skip to content

Commit d30e6b4

Browse files
committed
make it a member
1 parent e1402cf commit d30e6b4

File tree

2 files changed

+34
-45
lines changed

2 files changed

+34
-45
lines changed

mlir/lib/Dialect/EmitC/Transforms/AddReflectionMap.cpp

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "mlir/IR/BuiltinAttributes.h"
1414
#include "mlir/IR/PatternMatch.h"
1515
#include "mlir/Transforms/WalkPatternRewriteDriver.h"
16+
#include "llvm/Support/FormatVariadic.h"
1617

1718
using namespace mlir;
1819
using namespace emitc;
@@ -60,12 +61,11 @@ class AddReflectionMapPass
6061
}
6162

6263
mlir::OpBuilder builder(module.getBody(), module.getBody()->begin());
63-
if (!hasMapHdr) {
64+
if (!hasMapHdr)
6465
addHeader(builder, module, mapLibraryHeader);
65-
}
66-
if (!hasStringHdr) {
66+
67+
if (!hasStringHdr)
6768
addHeader(builder, module, stringLibraryHeader);
68-
}
6969
}
7070
};
7171

@@ -82,7 +82,6 @@ class AddReflectionMapClass : public OpRewritePattern<emitc::ClassOp> {
8282
PatternRewriter &rewriter) const override {
8383
MLIRContext *context = rewriter.getContext();
8484

85-
// Define the opaque types
8685
emitc::OpaqueType mapType = mlir::emitc::OpaqueType::get(
8786
context, "const std::map<std::string, char*>");
8887

@@ -105,43 +104,33 @@ class AddReflectionMapClass : public OpRewritePattern<emitc::ClassOp> {
105104
}
106105
});
107106

108-
std::stringstream ss;
109-
ss << "{ ";
107+
std::string mapString;
108+
mapString += "{ ";
110109
for (size_t i = 0; i < fieldNames.size(); ++i) {
111-
ss << " { \"" << fieldNames[i].first << "\", reinterpret_cast<char*>(&"
112-
<< fieldNames[i].second << ") }";
113-
if (i < fieldNames.size() - 1) {
114-
ss << ", ";
115-
}
110+
mapString += llvm::formatv("{ \"{0}\", reinterpret_cast<char*>(&{1}) }",
111+
fieldNames[i].first, fieldNames[i].second);
112+
if (i < fieldNames.size() - 1)
113+
mapString += ", ";
116114
}
117-
ss << " }";
115+
mapString += " }";
118116

119117
emitc::FuncOp executeFunc =
120118
classOp.lookupSymbol<mlir::emitc::FuncOp>("execute");
121119
if (executeFunc)
122120
rewriter.setInsertionPoint(executeFunc);
123-
else
121+
else {
124122
classOp.emitError() << "ClassOp must contain a function named 'execute' "
125123
"to add reflection map";
126-
127-
emitc::OpaqueType returnType = mlir::emitc::OpaqueType::get(
128-
context, "const std::map<std::string, char*>");
129-
130-
// Create the getFeatures function
131-
emitc::FuncOp getFeaturesFunc = rewriter.create<mlir::emitc::FuncOp>(
132-
classOp.getLoc(), "getFeatures",
133-
rewriter.getFunctionType({}, {returnType}));
134-
135-
// Add the body of the getFeatures function
136-
Block *funcBody = getFeaturesFunc.addEntryBlock();
137-
rewriter.setInsertionPointToStart(funcBody);
124+
return failure();
125+
}
138126

139127
// Create the constant map
140-
emitc::ConstantOp bufferMap = rewriter.create<emitc::ConstantOp>(
141-
classOp.getLoc(), mapType, emitc::OpaqueAttr::get(context, ss.str()));
128+
rewriter.create<emitc::ConstantOp>(
129+
classOp.getLoc(), mapType, emitc::OpaqueAttr::get(context, mapString));
142130

143-
rewriter.create<mlir::emitc::ReturnOp>(classOp.getLoc(),
144-
bufferMap.getResult());
131+
// TODO: Ideally, we would create a function that returns a reference to the
132+
// buffer map. However, current limitations in EmitC function support make
133+
// this difficult to implement at the moment.
145134

146135
return success();
147136
}

mlir/test/Dialect/EmitC/add_reflection_map.mlir

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ emitc.class @mainClass {
2727
// CHECK-NEXT: emitc.field @fieldName0 : !emitc.array<1xf32> {emitc.field_ref = ["another_feature"]}
2828
// CHECK-NEXT: emitc.field @fieldName1 : !emitc.array<1xf32> {emitc.field_ref = ["some_feature"]}
2929
// CHECK-NEXT: emitc.field @fieldName2 : !emitc.array<1xf32> {emitc.field_ref = ["output_0"]}
30-
// CHECK-NEXT: emitc.func @getFeatures() -> !emitc.opaque<"const std::map<std::string, char*>"> {
31-
// CHECK-NEXT: %0 = "emitc.constant"() <{value = #emitc.opaque<"{ { \22another_feature\22, reinterpret_cast<char*>(&fieldName0) }, { \22some_feature\22, reinterpret_cast<char*>(&fieldName1) }, { \22output_0\22, reinterpret_cast<char*>(&fieldName2) } }">}> : () -> !emitc.opaque<"const std::map<std::string, char*>">
32-
// CHECK-NEXT: return %0 : !emitc.opaque<"const std::map<std::string, char*>">
33-
// CHECK-NEXT: }
30+
// CHECK-NEXT: %0 = "emitc.constant"() <{value = #emitc.opaque<"{ { \22another_feature\22, reinterpret_cast<char*>(&fieldName0) },
31+
// CHECK-SAME: { \22some_feature\22, reinterpret_cast<char*>(&fieldName1) },
32+
// CHECK-SAME: { \22output_0\22, reinterpret_cast<char*>(&fieldName2) } }">}>
33+
// CHECK-SAME: : () -> !emitc.opaque<"const std::map<std::string, char*>">
3434
// CHECK-NEXT: emitc.func @execute() {
35-
// CHECK-NEXT: %0 = "emitc.constant"() <{value = 0 : index}> : () -> !emitc.size_t
36-
// CHECK-NEXT: %1 = get_field @fieldName0 : !emitc.array<1xf32>
37-
// CHECK-NEXT: %2 = get_field @fieldName1 : !emitc.array<1xf32>
38-
// CHECK-NEXT: %3 = get_field @fieldName2 : !emitc.array<1xf32>
39-
// CHECK-NEXT: %4 = subscript %2[%0] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
40-
// CHECK-NEXT: %5 = load %4 : <f32>
41-
// CHECK-NEXT: %6 = subscript %1[%0] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
42-
// CHECK-NEXT: %7 = load %6 : <f32>
43-
// CHECK-NEXT: %8 = add %5, %7 : (f32, f32) -> f32
44-
// CHECK-NEXT: %9 = subscript %3[%0] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
45-
// CHECK-NEXT: assign %8 : f32 to %9 : <f32>
35+
// CHECK-NEXT: %1 = "emitc.constant"() <{value = 0 : index}> : () -> !emitc.size_t
36+
// CHECK-NEXT: %2 = get_field @fieldName0 : !emitc.array<1xf32>
37+
// CHECK-NEXT: %3 = get_field @fieldName1 : !emitc.array<1xf32>
38+
// CHECK-NEXT: %4 = get_field @fieldName2 : !emitc.array<1xf32>
39+
// CHECK-NEXT: %5 = subscript %3[%1] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
40+
// CHECK-NEXT: %6 = load %5 : <f32>
41+
// CHECK-NEXT: %7 = subscript %2[%1] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
42+
// CHECK-NEXT: %8 = load %7 : <f32>
43+
// CHECK-NEXT: %9 = add %6, %8 : (f32, f32) -> f32
44+
// CHECK-NEXT: %10 = subscript %4[%1] : (!emitc.array<1xf32>, !emitc.size_t) -> !emitc.lvalue<f32>
45+
// CHECK-NEXT: assign %9 : f32 to %10 : <f32>
4646
// CHECK-NEXT: return
4747
// CHECK-NEXT: }
4848
// CHECK-NEXT: }

0 commit comments

Comments
 (0)