Skip to content

Commit efd3e89

Browse files
committed
[AutoBump] Merge with fixes of 4cc7d60 (Feb 18)
2 parents 6666d7e + 4cc7d60 commit efd3e89

File tree

7 files changed

+74
-74
lines changed

7 files changed

+74
-74
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,21 @@ def IntegerIndexOrOpaqueType : Type<CPred<"emitc::isIntegerIndexOrOpaqueType($_s
5757
"integer, index or opaque type supported by EmitC">;
5858
def FloatIntegerIndexOrOpaqueType : AnyTypeOf<[EmitCFloatType, IntegerIndexOrOpaqueType]>;
5959

60-
def EmitC_TranslationUnitOp : EmitC_Op<"tu",
61-
[IsolatedFromAbove, NoRegionArguments, SymbolTable,
62-
OpAsmOpInterface
63-
] # GraphRegionNoTerminator.traits> {
64-
let summary = "A translation unit container operation";
60+
def EmitC_FileOp
61+
: EmitC_Op<"file", [IsolatedFromAbove, NoRegionArguments, SymbolTable,
62+
OpAsmOpInterface]#GraphRegionNoTerminator.traits> {
63+
let summary = "A file container operation";
6564
let description = [{
66-
A `tu` represents a translation unit that can be emitted
67-
into a single C++ file.
65+
A `file` represents a single C/C++ file.
6866

69-
`mlir-translate` emits only the translation unit selected via
70-
the `-translation-unit-id=id` flag. By default, no translation units are
71-
emitted.
67+
`mlir-translate` ignores the body of all `emitc.file` ops
68+
unless the `-file-id=id` flag is used. With that flag, all `emitc.file` ops
69+
with matching id are emitted.
7270

7371
Example:
7472

7573
```mlir
76-
emitc.tu "main" {
74+
emitc.file "main" {
7775
emitc.func @func_one() {
7876
emitc.return
7977
}
@@ -87,15 +85,14 @@ def EmitC_TranslationUnitOp : EmitC_Op<"tu",
8785
let assemblyFormat = "$id attr-dict-with-keyword $bodyRegion";
8886
let builders = [OpBuilder<(ins CArg<"StringRef">:$id)>];
8987
let extraClassDeclaration = [{
90-
/// Construct a module from the given location with an optional name.
91-
static TranslationUnitOp create(Location loc, StringRef name);
88+
/// Construct a file op from the given location with a name.
89+
static FileOp create(Location loc, StringRef name);
9290

9391
//===------------------------------------------------------------------===//
9492
// OpAsmOpInterface Methods
9593
//===------------------------------------------------------------------===//
9694

97-
/// EmitC ops in the body of the translation_unit can omit their 'emitc.'
98-
/// prefix in the assembly.
95+
/// EmitC ops in the body can omit their 'emitc.' prefix in the assembly.
9996
static ::llvm::StringRef getDefaultDialect() {
10097
return "emitc";
10198
}

mlir/include/mlir/Target/Cpp/CppEmitter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ namespace emitc {
2424
/// the region of 'op' need almost all be in EmitC dialect. The parameter
2525
/// 'declareVariablesAtTop' enforces that all variables for op results and block
2626
/// arguments are declared at the beginning of the function.
27+
/// If parameter 'fileId' is non-empty, then body of `emitc.file` ops
28+
/// with matching id are emitted.
2729
LogicalResult translateToCpp(Operation *op, raw_ostream &os,
2830
bool declareVariablesAtTop = false,
29-
StringRef onlyTu = "",
31+
StringRef fileId = {},
3032
bool constantsAsVariables = true);
3133
} // namespace emitc
3234
} // namespace mlir

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,10 +1479,9 @@ void SwitchOp::getRegionInvocationBounds(
14791479
}
14801480

14811481
//===----------------------------------------------------------------------===//
1482-
// TranslationUnitOp
1482+
// FileOp
14831483
//===----------------------------------------------------------------------===//
1484-
void TranslationUnitOp::build(OpBuilder &builder, OperationState &state,
1485-
StringRef id) {
1484+
void FileOp::build(OpBuilder &builder, OperationState &state, StringRef id) {
14861485
state.addRegion()->emplaceBlock();
14871486
state.attributes.push_back(
14881487
builder.getNamedAttr("id", builder.getStringAttr(id)));

mlir/lib/Target/Cpp/TranslateRegistration.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ void registerToCppTranslation() {
2929
llvm::cl::desc("Declare variables at top when emitting C/C++"),
3030
llvm::cl::init(false));
3131

32-
static llvm::cl::opt<std::string> onlyTu(
33-
"translation-unit-id",
34-
llvm::cl::desc("Only emit the translation unit with the matching id"),
32+
static llvm::cl::opt<std::string> fileId(
33+
"file-id", llvm::cl::desc("Emit emitc.file ops with matching id"),
3534
llvm::cl::init(""));
3635

3736
static llvm::cl::opt<bool> constantsAsVariables(
@@ -45,7 +44,7 @@ void registerToCppTranslation() {
4544
return emitc::translateToCpp(
4645
op, output,
4746
/*declareVariablesAtTop=*/declareVariablesAtTop,
48-
/*onlyTu=*/onlyTu,
47+
/*fileId=*/fileId,
4948
/*constantsAsVariables=*/constantsAsVariables);
5049
},
5150
[](DialectRegistry &registry) {

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ namespace {
116116
/// Emitter that uses dialect specific emitters to emit C++ code.
117117
struct CppEmitter {
118118
explicit CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
119-
StringRef onlyTu, bool constantsAsVariables);
119+
StringRef fileId, bool constantsAsVariables);
120120

121121
/// Emits attribute or returns failure.
122122
LogicalResult emitAttribute(Location loc, Attribute attr);
@@ -257,7 +257,9 @@ struct CppEmitter {
257257
bool shouldDeclareVariablesAtTop() { return declareVariablesAtTop; };
258258

259259
/// Returns whether this translation unit should be emitted
260-
bool shouldEmitTu(TranslationUnitOp tu) { return tu.getId() == onlyTu; }
260+
bool shouldEmitFile(FileOp file) {
261+
return !fileId.empty() && file.getId() == fileId;
262+
}
261263

262264
/// Returns whether the value of ConstantOps should be stored in variables
263265
/// or emmited directly in their usage locations.
@@ -285,8 +287,8 @@ struct CppEmitter {
285287
/// taken care of by transformations run by the backend.
286288
bool shouldBeInlined(ExpressionOp expressionOp);
287289

288-
/// This emitter will only emit translation units whos id matches this value.
289-
StringRef willOnlyEmitTu() { return onlyTu; }
290+
/// This emitter will only emit a file whos id matches this value.
291+
StringRef willOnlyEmitFile() { return fileId; }
290292

291293
// Resets the value counter to 0
292294
void resetValueCounter();
@@ -309,8 +311,8 @@ struct CppEmitter {
309311
/// includes results from ops located in nested regions.
310312
bool declareVariablesAtTop;
311313

312-
/// Only emit translation units whos id matches this value.
313-
std::string onlyTu;
314+
/// Only emit file ops whos id matches this value.
315+
std::string fileId;
314316

315317
/// Use variables to hold the constant values
316318
bool constantsAsVariables;
@@ -431,7 +433,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
431433
/// Temporary emitter object that writes to our stream instead of the output
432434
/// allowing for the capture and caching of the produced string.
433435
CppEmitter sniffer = CppEmitter(ss, emitter.shouldDeclareVariablesAtTop(),
434-
emitter.willOnlyEmitTu(),
436+
emitter.willOnlyEmitFile(),
435437
emitter.shouldUseConstantsAsVariables());
436438

437439
ss << "(";
@@ -1091,11 +1093,11 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
10911093
return success();
10921094
}
10931095

1094-
static LogicalResult printOperation(CppEmitter &emitter, TranslationUnitOp tu) {
1095-
if (!emitter.shouldEmitTu(tu))
1096+
static LogicalResult printOperation(CppEmitter &emitter, FileOp file) {
1097+
if (!emitter.shouldEmitFile(file))
10961098
return success();
10971099

1098-
for (Operation &op : tu) {
1100+
for (Operation &op : file) {
10991101
if (failed(emitter.emitOperation(op, /*trailingSemicolon=*/false)))
11001102
return failure();
11011103
}
@@ -1348,9 +1350,9 @@ static LogicalResult printOperation(CppEmitter &emitter,
13481350
}
13491351

13501352
CppEmitter::CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
1351-
StringRef onlyTu, bool constantsAsVariables)
1353+
StringRef fileId, bool constantsAsVariables)
13521354
: os(os), declareVariablesAtTop(declareVariablesAtTop),
1353-
onlyTu(onlyTu.str()), constantsAsVariables(constantsAsVariables),
1355+
fileId(fileId.str()), constantsAsVariables(constantsAsVariables),
13541356
defaultValueMapperScope(valueMapper),
13551357
defaultBlockMapperScope(blockMapper) {
13561358
labelInScopeCount.push(0);
@@ -1778,11 +1780,11 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
17781780
emitc::BitwiseRightShiftOp, emitc::BitwiseXorOp, emitc::CallOp,
17791781
emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
17801782
emitc::ConditionalOp, emitc::ConstantOp, emitc::DeclareFuncOp,
1781-
emitc::DivOp, emitc::ExpressionOp, emitc::ForOp, emitc::FuncOp,
1782-
emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp, emitc::LoadOp,
1783-
emitc::LogicalAndOp, emitc::LogicalNotOp, emitc::LogicalOrOp,
1784-
emitc::MulOp, emitc::RemOp, emitc::ReturnOp, emitc::SubOp,
1785-
emitc::SwitchOp, emitc::TranslationUnitOp, emitc::UnaryMinusOp,
1783+
emitc::DivOp, emitc::ExpressionOp, emitc::FileOp, emitc::ForOp,
1784+
emitc::FuncOp, emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp,
1785+
emitc::LoadOp, emitc::LogicalAndOp, emitc::LogicalNotOp,
1786+
emitc::LogicalOrOp, emitc::MulOp, emitc::RemOp, emitc::ReturnOp,
1787+
emitc::SubOp, emitc::SwitchOp, emitc::UnaryMinusOp,
17861788
emitc::UnaryPlusOp, emitc::VariableOp, emitc::VerbatimOp>(
17871789
[&](auto op) { return printOperation(*this, op); })
17881790
// Func ops.
@@ -1814,16 +1816,17 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
18141816
if (hasDeferredEmission(&op))
18151817
return success();
18161818

1817-
if (isa<ModuleOp, TranslationUnitOp>(op))
1819+
if (isa<ModuleOp, FileOp>(op))
18181820
return success(); // skip adding newlines
18191821

18201822
if (getEmittedExpression() ||
18211823
(isa<emitc::ExpressionOp>(op) &&
18221824
shouldBeInlined(cast<emitc::ExpressionOp>(op))))
18231825
return success();
18241826

1825-
if (isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp, emitc::IfOp,
1826-
emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(op)) {
1827+
if (isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::FileOp, emitc::ForOp,
1828+
emitc::IfOp, emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(
1829+
op)) {
18271830
trailingSemicolon = false;
18281831
}
18291832

@@ -2015,8 +2018,8 @@ void CppEmitter::decreaseLoopNestingLevel() { loopNestingLevel--; }
20152018

20162019
LogicalResult emitc::translateToCpp(Operation *op, raw_ostream &os,
20172020
bool declareVariablesAtTop,
2018-
StringRef onlyTu,
2021+
StringRef fileId,
20192022
bool constantsAsVariables) {
2020-
CppEmitter emitter(os, declareVariablesAtTop, onlyTu, constantsAsVariables);
2023+
CppEmitter emitter(os, declareVariablesAtTop, fileId, constantsAsVariables);
20212024
return emitter.emitOperation(*op, /*trailingSemicolon=*/false);
20222025
}

mlir/test/Target/Cpp/file.mlir

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s --check-prefix NO-FILTER --allow-empty
2+
// RUN: mlir-translate -mlir-to-cpp -file-id=non-existing %s | FileCheck %s --check-prefix NON-EXISTING --allow-empty
3+
// RUN: mlir-translate -mlir-to-cpp -file-id=file_one %s | FileCheck %s --check-prefix FILE-ONE --allow-empty
4+
// RUN: mlir-translate -mlir-to-cpp -file-id=file_two %s | FileCheck %s --check-prefix FILE-TWO --allow-empty
5+
6+
7+
// NO-FILTER-NOT: func_one
8+
// NO-FILTER-NOT: func_two
9+
10+
// NON-EXISTING-NOT: func_one
11+
// NON-EXISTING-NOT: func_two
12+
13+
// FILE-ONE: func_one
14+
// FILE-ONE-NOT: func_two
15+
16+
// FILE-TWO-NOT: func_one
17+
// FILE-TWO: func_two
18+
19+
emitc.file "file_one" {
20+
emitc.func @func_one(%arg: f32) {
21+
emitc.return
22+
}
23+
}
24+
25+
emitc.file "file_two" {
26+
emitc.func @func_two(%arg: f32) {
27+
emitc.return
28+
}
29+
}

mlir/test/Target/Cpp/tu.mlir

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)