Enzyme main
Loading...
Searching...
No Matches
enzymemlir-translate.cpp
Go to the documentation of this file.
1//===- enzymemlir-translate.cpp - The enzymemlir-translate driver ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the 'enzymemlir-translate' tool, which is the enzyme
10// analog of mlir-translate, used to drive lowering to LLVM IR.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Dialect/Dialect.h"
15
16#include "mlir/Dialect/DLTI/DLTI.h"
17#include "mlir/Dialect/Func/IR/FuncOps.h"
18#include "mlir/InitAllTranslations.h"
19#include "mlir/Support/LogicalResult.h"
20#include "mlir/Target/LLVMIR/Dialect/All.h"
21#include "mlir/Target/LLVMIR/Export.h"
22#include "mlir/Target/LLVMIR/ModuleTranslation.h"
23#include "mlir/Tools/mlir-translate/MlirTranslateMain.h"
24#include "mlir/Tools/mlir-translate/Translation.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Module.h"
27
28#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
29
30using namespace mlir;
31using namespace llvm;
33 using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
34
35 void annotateActivity(StringRef key,
36 ArrayRef<llvm::Instruction *> instructions) const {
37 if (instructions.empty())
38 return;
39
40 LLVMContext &llvmCtx = instructions.front()->getContext();
41 MDNode *md = MDNode::get(llvmCtx, {});
42 for (llvm::Instruction *inst : instructions) {
43 inst->setMetadata(key, md);
44 }
45 }
46
47 LogicalResult
48 amendOperation(Operation *op, ArrayRef<llvm::Instruction *> instructions,
49 NamedAttribute attribute,
50 LLVM::ModuleTranslation &moduleTranslation) const override {
51 if (auto funcOp = dyn_cast<FunctionOpInterface>(op))
52 return success();
53
54 auto iciAttr = op->getAttrOfType<BoolAttr>("enzyme.ici");
55 auto icvAttr = op->getAttrOfType<BoolAttr>("enzyme.icv");
56
57 // Op was already processed.
58 if (!(iciAttr && icvAttr))
59 return success();
60
61 // Convert the attributes to the appropriate metadata.
62 if (iciAttr.getValue() && icvAttr.getValue()) {
63 annotateActivity("enzyme_inactive", instructions);
64 } else if (!iciAttr.getValue() && !icvAttr.getValue()) {
65 annotateActivity("enzyme_active", instructions);
66 } else {
67 StringRef instActivity =
68 iciAttr.getValue() ? "enzyme_inactive_inst" : "enzyme_active_inst";
69 StringRef valActivity =
70 icvAttr.getValue() ? "enzyme_inactive_val" : "enzyme_active_val";
71 annotateActivity(instActivity, instructions);
72 annotateActivity(valActivity, instructions);
73 }
74
75 op->removeAttr("enzyme.ici");
76 op->removeAttr("enzyme.icv");
77 return success();
78 }
79};
80
81int main(int argc, char **argv) {
82 mlir::registerAllTranslations();
83
84 mlir::TranslateFromMLIRRegistration withdescription(
85 "activity-to-llvm", "different from option",
86 [](mlir::Operation *op, llvm::raw_ostream &output) {
87 llvm::LLVMContext llvmContext;
88 auto llvmModule = translateModuleToLLVMIR(op, llvmContext);
89 if (!llvmModule)
90 return failure();
91
92 llvmModule->print(output, nullptr);
93 return success();
94 },
95 [](mlir::DialectRegistry &registry) {
96 registry
97 .insert<DLTIDialect, func::FuncDialect, enzyme::EnzymeDialect>();
98 registerAllToLLVMIRTranslations(registry);
99 registry.addExtension(
100 +[](MLIRContext *ctx, enzyme::EnzymeDialect *dialect) {
101 dialect->addInterfaces<ActivityToMetadataTranslation>();
102 });
103 });
104
105 return failed(mlir::mlirTranslateMain(
106 argc, argv, "Enzyme MLIR Translation Testing Tool"));
107}
int main(int argc, char **argv)