diff options
author | River Riddle <riverriddle@google.com> | 2019-07-01 10:29:09 -0700 |
---|---|---|
committer | jpienaar <jpienaar@google.com> | 2019-07-01 11:39:00 -0700 |
commit | 54cd6a7e97a226738e2c85b86559918dd9e3cd5d (patch) | |
tree | affa803347d6695be575137d1ad55a055a8021e3 /mlir/bindings/python/pybind.cpp | |
parent | 84bd67fc4fd116e80f7a66bfadfe9a7fd6fd5e82 (diff) | |
download | bcm5719-llvm-54cd6a7e97a226738e2c85b86559918dd9e3cd5d.tar.gz bcm5719-llvm-54cd6a7e97a226738e2c85b86559918dd9e3cd5d.zip |
NFC: Refactor Function to be value typed.
Move the data members out of Function and into a new impl storage class 'FunctionStorage'. This allows for Function to become value typed, which will greatly simplify the transition of Function to FuncOp(given that FuncOp is also value typed).
PiperOrigin-RevId: 255983022
Diffstat (limited to 'mlir/bindings/python/pybind.cpp')
-rw-r--r-- | mlir/bindings/python/pybind.cpp | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/mlir/bindings/python/pybind.cpp b/mlir/bindings/python/pybind.cpp index 222ef52b9be..cdf4a7fe89c 100644 --- a/mlir/bindings/python/pybind.cpp +++ b/mlir/bindings/python/pybind.cpp @@ -17,6 +17,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" +#include "llvm/IR/Function.h" #include "llvm/IR/Module.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" @@ -110,13 +111,14 @@ struct PythonValueHandle { struct PythonFunction { PythonFunction() : function{nullptr} {} PythonFunction(mlir_func_t f) : function{f} {} - PythonFunction(mlir::Function *f) : function{f} {} + PythonFunction(mlir::Function f) + : function(const_cast<void *>(f.getAsOpaquePointer())) {} operator mlir_func_t() { return function; } std::string str() { - mlir::Function *f = reinterpret_cast<mlir::Function *>(function); + mlir::Function f = mlir::Function::getFromOpaquePointer(function); std::string res; llvm::raw_string_ostream os(res); - f->print(os); + f.print(os); return res; } @@ -124,18 +126,18 @@ struct PythonFunction { // declaration, add the entry block, transforming the declaration into a // definition. Return true if the block was added, false otherwise. bool define() { - auto *f = reinterpret_cast<mlir::Function *>(function); - if (!f->getBlocks().empty()) + auto f = mlir::Function::getFromOpaquePointer(function); + if (!f.getBlocks().empty()) return false; - f->addEntryBlock(); + f.addEntryBlock(); return true; } PythonValueHandle arg(unsigned index) { - Function *f = static_cast<Function *>(function); - assert(index < f->getNumArguments() && "argument index out of bounds"); - return PythonValueHandle(ValueHandle(f->getArgument(index))); + auto f = mlir::Function::getFromOpaquePointer(function); + assert(index < f.getNumArguments() && "argument index out of bounds"); + return PythonValueHandle(ValueHandle(f.getArgument(index))); } mlir_func_t function; @@ -250,10 +252,9 @@ struct PythonFunctionContext { PythonFunction enter() { assert(function.function && "function is not set up"); - auto *mlirFunc = static_cast<mlir::Function *>(function.function); - contextBuilder.emplace(mlirFunc->getBody()); - context = - new mlir::edsc::ScopedContext(*contextBuilder, mlirFunc->getLoc()); + auto mlirFunc = mlir::Function::getFromOpaquePointer(function.function); + contextBuilder.emplace(mlirFunc.getBody()); + context = new mlir::edsc::ScopedContext(*contextBuilder, mlirFunc.getLoc()); return function; } @@ -594,7 +595,7 @@ PythonMLIRModule::declareFunction(const std::string &name, } // Create the function itself. - auto *func = new mlir::Function( + auto func = mlir::Function::create( UnknownLoc::get(&mlirContext), name, mlir::Type::getFromOpaquePointer(funcType).cast<FunctionType>(), attrs, inputAttrs); @@ -652,9 +653,9 @@ PYBIND11_MODULE(pybind, m) { return ValueHandle::create<ConstantFloatOp>(value, floatType); }); m.def("constant_function", [](PythonFunction func) -> PythonValueHandle { - auto *function = reinterpret_cast<Function *>(func.function); - auto attr = FunctionAttr::get(function); - return ValueHandle::create<ConstantOp>(function->getType(), attr); + auto function = Function::getFromOpaquePointer(func.function); + auto attr = FunctionAttr::get(function.getName(), function.getContext()); + return ValueHandle::create<ConstantOp>(function.getType(), attr); }); m.def("appendTo", [](const PythonBlockHandle &handle) { return PythonBlockAppender(handle); |