Files
clang-p2996/mlir/lib/Target/Cpp/TranslateRegistration.cpp
Matthias Gehre 4cc7d60fe3 [MLIR] emitc: Add emitc.file op (#123298)
A `emitc.file` represents a file that can be emitted
into a single C++ file.

This allows to manage multiple source files within the same MLIR module,
but emit them into separate files.

This feature is opt-in.
By default, `mlir-translate` emits all ops outside of `emitc.file`
and ignores all `emitc.file` ops and their bodies.

When specifying the `-file-id=id` flag,
`mlir-translate` emits all ops outside of `emitc.file` and
the ops within the `emitc.file` with matching `id`.

Example:

```mlir
emitc.file "main" {
  func @func_one() {
    return
  }
}
emitc.file "test" {
  func @func_two() {
   return
  }
}
```

`mlir-translate -file-id=main` will emit `func_one` and
`mlir-translate -file-id=test` will emit `func_two`.
2025-02-18 15:21:17 +01:00

54 lines
1.8 KiB
C++

//===- TranslateRegistration.cpp - Register translation -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/ControlFlow/IR/ControlFlow.h"
#include "mlir/Dialect/EmitC/IR/EmitC.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Dialect.h"
#include "mlir/Target/Cpp/CppEmitter.h"
#include "mlir/Tools/mlir-translate/Translation.h"
#include "llvm/Support/CommandLine.h"
using namespace mlir;
namespace mlir {
//===----------------------------------------------------------------------===//
// Cpp registration
//===----------------------------------------------------------------------===//
void registerToCppTranslation() {
static llvm::cl::opt<bool> declareVariablesAtTop(
"declare-variables-at-top",
llvm::cl::desc("Declare variables at top when emitting C/C++"),
llvm::cl::init(false));
static llvm::cl::opt<std::string> fileId(
"file-id", llvm::cl::desc("Emit emitc.file ops with matching id"),
llvm::cl::init(""));
TranslateFromMLIRRegistration reg(
"mlir-to-cpp", "translate from mlir to cpp",
[](Operation *op, raw_ostream &output) {
return emitc::translateToCpp(
op, output,
/*declareVariablesAtTop=*/declareVariablesAtTop,
/*fileId=*/fileId);
},
[](DialectRegistry &registry) {
// clang-format off
registry.insert<cf::ControlFlowDialect,
emitc::EmitCDialect,
func::FuncDialect>();
// clang-format on
});
}
} // namespace mlir