Skip to content

[AutoBump] Merge with fixes of 4cc7d60f (Feb 18) (51) #595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: bump_to_27fe2c95
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,21 @@ def IntegerIndexOrOpaqueType : Type<CPred<"emitc::isIntegerIndexOrOpaqueType($_s
"integer, index or opaque type supported by EmitC">;
def FloatIntegerIndexOrOpaqueType : AnyTypeOf<[EmitCFloatType, IntegerIndexOrOpaqueType]>;

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

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

Example:

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

//===------------------------------------------------------------------===//
// OpAsmOpInterface Methods
//===------------------------------------------------------------------===//

/// EmitC ops in the body of the translation_unit can omit their 'emitc.'
/// prefix in the assembly.
/// EmitC ops in the body can omit their 'emitc.' prefix in the assembly.
static ::llvm::StringRef getDefaultDialect() {
return "emitc";
}
Expand Down
4 changes: 3 additions & 1 deletion mlir/include/mlir/Target/Cpp/CppEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ namespace emitc {
/// the region of 'op' need almost all be in EmitC dialect. The parameter
/// 'declareVariablesAtTop' enforces that all variables for op results and block
/// arguments are declared at the beginning of the function.
/// If parameter 'fileId' is non-empty, then body of `emitc.file` ops
/// with matching id are emitted.
LogicalResult translateToCpp(Operation *op, raw_ostream &os,
bool declareVariablesAtTop = false,
StringRef onlyTu = "",
StringRef fileId = {},
bool constantsAsVariables = true);
} // namespace emitc
} // namespace mlir
Expand Down
5 changes: 2 additions & 3 deletions mlir/lib/Dialect/EmitC/IR/EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1479,10 +1479,9 @@ void SwitchOp::getRegionInvocationBounds(
}

//===----------------------------------------------------------------------===//
// TranslationUnitOp
// FileOp
//===----------------------------------------------------------------------===//
void TranslationUnitOp::build(OpBuilder &builder, OperationState &state,
StringRef id) {
void FileOp::build(OpBuilder &builder, OperationState &state, StringRef id) {
state.addRegion()->emplaceBlock();
state.attributes.push_back(
builder.getNamedAttr("id", builder.getStringAttr(id)));
Expand Down
7 changes: 3 additions & 4 deletions mlir/lib/Target/Cpp/TranslateRegistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ void registerToCppTranslation() {
llvm::cl::desc("Declare variables at top when emitting C/C++"),
llvm::cl::init(false));

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

static llvm::cl::opt<bool> constantsAsVariables(
Expand All @@ -45,7 +44,7 @@ void registerToCppTranslation() {
return emitc::translateToCpp(
op, output,
/*declareVariablesAtTop=*/declareVariablesAtTop,
/*onlyTu=*/onlyTu,
/*fileId=*/fileId,
/*constantsAsVariables=*/constantsAsVariables);
},
[](DialectRegistry &registry) {
Expand Down
47 changes: 25 additions & 22 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ namespace {
/// Emitter that uses dialect specific emitters to emit C++ code.
struct CppEmitter {
explicit CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
StringRef onlyTu, bool constantsAsVariables);
StringRef fileId, bool constantsAsVariables);

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

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

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

/// This emitter will only emit translation units whos id matches this value.
StringRef willOnlyEmitTu() { return onlyTu; }
/// This emitter will only emit a file whos id matches this value.
StringRef willOnlyEmitFile() { return fileId; }

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

/// Only emit translation units whos id matches this value.
std::string onlyTu;
/// Only emit file ops whos id matches this value.
std::string fileId;

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

ss << "(";
Expand Down Expand Up @@ -1091,11 +1093,11 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
return success();
}

static LogicalResult printOperation(CppEmitter &emitter, TranslationUnitOp tu) {
if (!emitter.shouldEmitTu(tu))
static LogicalResult printOperation(CppEmitter &emitter, FileOp file) {
if (!emitter.shouldEmitFile(file))
return success();

for (Operation &op : tu) {
for (Operation &op : file) {
if (failed(emitter.emitOperation(op, /*trailingSemicolon=*/false)))
return failure();
}
Expand Down Expand Up @@ -1348,9 +1350,9 @@ static LogicalResult printOperation(CppEmitter &emitter,
}

CppEmitter::CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
StringRef onlyTu, bool constantsAsVariables)
StringRef fileId, bool constantsAsVariables)
: os(os), declareVariablesAtTop(declareVariablesAtTop),
onlyTu(onlyTu.str()), constantsAsVariables(constantsAsVariables),
fileId(fileId.str()), constantsAsVariables(constantsAsVariables),
defaultValueMapperScope(valueMapper),
defaultBlockMapperScope(blockMapper) {
labelInScopeCount.push(0);
Expand Down Expand Up @@ -1778,11 +1780,11 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
emitc::BitwiseRightShiftOp, emitc::BitwiseXorOp, emitc::CallOp,
emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
emitc::ConditionalOp, emitc::ConstantOp, emitc::DeclareFuncOp,
emitc::DivOp, emitc::ExpressionOp, emitc::ForOp, emitc::FuncOp,
emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp, emitc::LoadOp,
emitc::LogicalAndOp, emitc::LogicalNotOp, emitc::LogicalOrOp,
emitc::MulOp, emitc::RemOp, emitc::ReturnOp, emitc::SubOp,
emitc::SwitchOp, emitc::TranslationUnitOp, emitc::UnaryMinusOp,
emitc::DivOp, emitc::ExpressionOp, emitc::FileOp, emitc::ForOp,
emitc::FuncOp, emitc::GlobalOp, emitc::IfOp, emitc::IncludeOp,
emitc::LoadOp, emitc::LogicalAndOp, emitc::LogicalNotOp,
emitc::LogicalOrOp, emitc::MulOp, emitc::RemOp, emitc::ReturnOp,
emitc::SubOp, emitc::SwitchOp, emitc::UnaryMinusOp,
emitc::UnaryPlusOp, emitc::VariableOp, emitc::VerbatimOp>(
[&](auto op) { return printOperation(*this, op); })
// Func ops.
Expand Down Expand Up @@ -1814,16 +1816,17 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
if (hasDeferredEmission(&op))
return success();

if (isa<ModuleOp, TranslationUnitOp>(op))
if (isa<ModuleOp, FileOp>(op))
return success(); // skip adding newlines

if (getEmittedExpression() ||
(isa<emitc::ExpressionOp>(op) &&
shouldBeInlined(cast<emitc::ExpressionOp>(op))))
return success();

if (isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::ForOp, emitc::IfOp,
emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(op)) {
if (isa<cf::CondBranchOp, emitc::DeclareFuncOp, emitc::FileOp, emitc::ForOp,
emitc::IfOp, emitc::IncludeOp, emitc::SwitchOp, emitc::VerbatimOp>(
op)) {
trailingSemicolon = false;
}

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

LogicalResult emitc::translateToCpp(Operation *op, raw_ostream &os,
bool declareVariablesAtTop,
StringRef onlyTu,
StringRef fileId,
bool constantsAsVariables) {
CppEmitter emitter(os, declareVariablesAtTop, onlyTu, constantsAsVariables);
CppEmitter emitter(os, declareVariablesAtTop, fileId, constantsAsVariables);
return emitter.emitOperation(*op, /*trailingSemicolon=*/false);
}
29 changes: 29 additions & 0 deletions mlir/test/Target/Cpp/file.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s --check-prefix NO-FILTER --allow-empty
// RUN: mlir-translate -mlir-to-cpp -file-id=non-existing %s | FileCheck %s --check-prefix NON-EXISTING --allow-empty
// RUN: mlir-translate -mlir-to-cpp -file-id=file_one %s | FileCheck %s --check-prefix FILE-ONE --allow-empty
// RUN: mlir-translate -mlir-to-cpp -file-id=file_two %s | FileCheck %s --check-prefix FILE-TWO --allow-empty


// NO-FILTER-NOT: func_one
// NO-FILTER-NOT: func_two

// NON-EXISTING-NOT: func_one
// NON-EXISTING-NOT: func_two

// FILE-ONE: func_one
// FILE-ONE-NOT: func_two

// FILE-TWO-NOT: func_one
// FILE-TWO: func_two

emitc.file "file_one" {
emitc.func @func_one(%arg: f32) {
emitc.return
}
}

emitc.file "file_two" {
emitc.func @func_two(%arg: f32) {
emitc.return
}
}
29 changes: 0 additions & 29 deletions mlir/test/Target/Cpp/tu.mlir

This file was deleted.