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
1718using namespace mlir ;
1819using 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 }
0 commit comments