Skip to content

Commit e3c958a

Browse files
committed
small change
1 parent afe86ff commit e3c958a

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

mlir/include/mlir/Dialect/EmitC/Transforms/Passes.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def AddReflectionMapPass : Pass<"add-reflection-map", "ModuleOp"> {
5959
let description = [{
6060
This pass adds a `getBufferForName` function to EmitC classes that enables
6161
runtime lookup of field buffers by their string names.
62-
This would require that the class has fields with attributes and a function named `execute`.
62+
This requires that the class has fields with attributes and a function named `execute`.
6363
The `fieldop` attribute is expected to be a dictionary where:
6464
- The keys are `namedAttribute`.
6565
- The values are arrays containing a single string attribute.

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ namespace emitc {
2323
#include "mlir/Dialect/EmitC/Transforms/Passes.h.inc"
2424

2525
namespace {
26-
constexpr const char *kMapLibraryHeader = "map";
27-
constexpr const char *kStringLibraryHeader = "string";
26+
constexpr const char *mapLibraryHeader = "map";
27+
constexpr const char *stringLibraryHeader = "string";
28+
29+
IncludeOp addHeader(OpBuilder &builder, ModuleOp module, StringRef headerName) {
30+
StringAttr includeAttr = builder.getStringAttr(headerName);
31+
return builder.create<emitc::IncludeOp>(
32+
module.getLoc(), includeAttr,
33+
/*is_standard_include=*/builder.getUnitAttr());
34+
}
35+
2836
class AddReflectionMapPass
2937
: public impl::AddReflectionMapPassBase<AddReflectionMapPass> {
3038
using AddReflectionMapPassBase::AddReflectionMapPassBase;
@@ -35,35 +43,28 @@ class AddReflectionMapPass
3543
populateAddReflectionMapPatterns(patterns, namedAttribute);
3644

3745
walkAndApplyPatterns(module, std::move(patterns));
38-
bool hasMap = false;
39-
bool hasString = false;
46+
bool hasMapHdr = false;
47+
bool hasStringHdr = false;
4048
for (auto &op : *module.getBody()) {
4149
emitc::IncludeOp includeOp = llvm::dyn_cast<mlir::emitc::IncludeOp>(op);
4250
if (!includeOp)
4351
continue;
4452
if (includeOp.getIsStandardInclude()) {
45-
if (includeOp.getInclude() == kMapLibraryHeader)
46-
hasMap = true;
47-
if (includeOp.getInclude() == kStringLibraryHeader)
48-
hasString = true;
53+
if (includeOp.getInclude() == mapLibraryHeader)
54+
hasMapHdr = true;
55+
if (includeOp.getInclude() == stringLibraryHeader)
56+
hasStringHdr = true;
4957
}
58+
if (hasMapHdr && hasStringHdr)
59+
return;
5060
}
5161

52-
if (hasMap && hasString)
53-
return;
54-
5562
mlir::OpBuilder builder(module.getBody(), module.getBody()->begin());
56-
if (!hasMap) {
57-
StringAttr includeAttr = builder.getStringAttr(kMapLibraryHeader);
58-
builder.create<mlir::emitc::IncludeOp>(
59-
module.getLoc(), includeAttr,
60-
/*is_standard_include=*/builder.getUnitAttr());
63+
if (!hasMapHdr) {
64+
addHeader(builder, module, mapLibraryHeader);
6165
}
62-
if (!hasString) {
63-
StringAttr includeAttr = builder.getStringAttr(kStringLibraryHeader);
64-
builder.create<emitc::IncludeOp>(
65-
module.getLoc(), includeAttr,
66-
/*is_standard_include=*/builder.getUnitAttr());
66+
if (!hasStringHdr) {
67+
addHeader(builder, module, stringLibraryHeader);
6768
}
6869
}
6970
};

0 commit comments

Comments
 (0)