LLVM IR Dialect: separate the conversion tool from the conversion pass
authorAlex Zinenko <zinenko@google.com>
Thu, 28 Mar 2019 23:10:07 +0000 (16:10 -0700)
committerjpienaar <jpienaar@google.com>
Sat, 30 Mar 2019 00:52:20 +0000 (17:52 -0700)
Originally, the conversion to the LLVM IR dialect had been implemented as pass.
The common conversion infrastructure was factored into DialectConversion from
which the conversion pass inherited.  The conversion being a pass is
undesirable for callers that only need the conversion done, for example as a
part of sequence of conversions or outside the pass manager infrastructure.
Split the LLVM IR Dialect conversion into the conversion proper and the
conversion pass, where the latter contains the former instead of inheriting.
NFC.

PiperOrigin-RevId: 240874740

mlir/include/mlir/LLVMIR/Transforms.h
mlir/lib/LLVMIR/Transforms/ConvertToLLVMDialect.cpp

index 3c62f19..87a7a0e 100644 (file)
 #ifndef MLIR_LLVMIR_TRANSFORMS_H_
 #define MLIR_LLVMIR_TRANSFORMS_H_
 
+#include <memory>
+
 namespace mlir {
+class DialectConversion;
+class Module;
 class ModulePassBase;
 
 /// Creates a pass to convert Standard dialects into the LLVMIR dialect.
 ModulePassBase *createConvertToLLVMIRPass();
 
+/// Creates a dialect converter from the standard dialect to the LLVM IR
+/// dialect and transfers ownership to the caller.
+std::unique_ptr<DialectConversion> createStdToLLVMConverter();
+
+namespace LLVM {
+/// Make argument-taking successors of each block distinct.  PHI nodes in LLVM
+/// IR use the predecessor ID to identify which value to take.  They do not
+/// support different values coming from the same predecessor.  If a block has
+/// another block as a successor more than once with different values, insert
+/// a new dummy block for LLVM PHI nodes to tell the sources apart.
+void ensureDistinctSuccessors(Module *m);
+} // namespace LLVM
+
 } // namespace mlir
 
 #endif // MLIR_LLVMIR_TRANSFORMS_H_
index ed3e2c4..d67e868 100644 (file)
@@ -1072,25 +1072,16 @@ static void ensureDistinctSuccessors(Block &bb) {
   }
 }
 
-static void ensureDistinctSuccessors(Module *m) {
+void mlir::LLVM::ensureDistinctSuccessors(Module *m) {
   for (auto &f : *m) {
     for (auto &bb : f.getBlocks()) {
-      ensureDistinctSuccessors(bb);
+      ::ensureDistinctSuccessors(bb);
     }
   }
 };
 
-/// A pass converting MLIR Standard and Builtin operations into the LLVM IR
-/// dialect.
-class LLVMLowering : public ModulePass<LLVMLowering>, public DialectConversion {
-public:
-  void runOnModule() override {
-    Module *m = &getModule();
-    uniqueSuccessorsWithArguments(m);
-    if (failed(DialectConversion::convert(m)))
-      signalPassFailure();
-  }
-
+/// A dialect converter from the Standard dialect to the LLVM IR dialect.
+class LLVMLowering : public DialectConversion {
 protected:
   // Create a set of converters that live in the pass object by passing them a
   // reference to the LLVM IR dialect.  Store the module associated with the
@@ -1136,15 +1127,6 @@ protected:
     return TypeConverter::convertFunctionSignature(t, *module);
   }
 
-  // Make argument-taking successors of each block distinct.  PHI nodes in LLVM
-  // IR use the predecessor ID to identify which value to take.  They do not
-  // support different values coming from the same predecessor.  If a block has
-  // another block as a successor more than once with different values, insert
-  // a new dummy block for LLVM PHI nodes to tell the sources apart.
-  void uniqueSuccessorsWithArguments(Module *m) {
-    return ensureDistinctSuccessors(m);
-  }
-
 private:
   // Storage for the conversion patterns.
   llvm::BumpPtrAllocator converterStorage;
@@ -1152,7 +1134,28 @@ private:
   llvm::Module *module;
 };
 
-ModulePassBase *mlir::createConvertToLLVMIRPass() { return new LLVMLowering(); }
+/// A pass converting MLIR Standard operations into the LLVM IR dialect.
+class LLVMLoweringPass : public ModulePass<LLVMLoweringPass> {
+public:
+  // Run the dialect converter on the module.
+  void runOnModule() override {
+    Module *m = &getModule();
+    LLVM::ensureDistinctSuccessors(m);
+    if (failed(impl.convert(m)))
+      signalPassFailure();
+  }
+
+private:
+  LLVMLowering impl;
+};
+
+ModulePassBase *mlir::createConvertToLLVMIRPass() {
+  return new LLVMLoweringPass();
+}
+
+std::unique_ptr<DialectConversion> mlir::createStdToLLVMConverter() {
+  return llvm::make_unique<LLVMLowering>();
+}
 
-static PassRegistration<LLVMLowering>
+static PassRegistration<LLVMLoweringPass>
     pass("convert-to-llvmir", "Convert all functions to the LLVM IR dialect");