Skip to content

Commit 4e55dbe

Browse files
authored
Merge pull request #19 from gf712/mlir-backend-improvements
Mlir backend improvements
2 parents 2606b6d + f2d126b commit 4e55dbe

65 files changed

Lines changed: 16486 additions & 15237 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/premerge.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ jobs:
2222
run: |
2323
wget https://apt.llvm.org/llvm.sh
2424
chmod +x llvm.sh
25-
sudo ./llvm.sh 20
26-
sudo apt install libmlir-20-dev mlir-20-tools
25+
sudo ./llvm.sh 23
26+
sudo apt install libmlir-23-dev mlir-23-tools
2727
2828
- name: ccache
2929
uses: hendrikmuhs/ccache-action@v1.2

CMakePresets.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
"CMAKE_BUILD_TYPE": "Release",
2121
"CPM_SOURCE_CACHE": ".cache/CPM"
2222
}
23+
},
24+
{
25+
"name": "release-with-debug-info",
26+
"displayName": "Release with Debug Info",
27+
"generator": "Ninja",
28+
"binaryDir": "build/release-with-debug-info",
29+
"cacheVariables": {
30+
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
31+
"CPM_SOURCE_CACHE": ".cache/CPM"
32+
}
2333
}
2434
],
2535
"buildPresets": [
@@ -34,6 +44,12 @@
3444
"displayName": "Release Build",
3545
"configurePreset": "release",
3646
"configuration": "Release"
47+
},
48+
{
49+
"name": "release-with-debug-info",
50+
"displayName": "Release with Debug Info Build",
51+
"configurePreset": "release-with-debug-info",
52+
"configuration": "RelWithDebInfo"
3753
}
3854
],
3955
"testPresets": [
@@ -46,6 +62,11 @@
4662
"name": "release",
4763
"displayName": "Test all in Release mode",
4864
"configurePreset": "release"
65+
},
66+
{
67+
"name": "release-with-debug-info",
68+
"displayName": "Test all in Release with Debug Info mode",
69+
"configurePreset": "release-with-debug-info"
4970
}
5071
]
5172

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ target_link_libraries(python-cpp
298298
)
299299

300300
# LLVM backend
301-
find_package(LLVM CONFIG 20.1)
301+
find_package(LLVM CONFIG 23)
302302
if(ENABLE_LLVM_BACKEND AND NOT LLVM_FOUND)
303303
message(FATAL_ERROR "Could not find LLVM in the local environment")
304304
elseif(ENABLE_LLVM_BACKEND AND LLVM_FOUND)

src/executable/bytecode/Bytecode.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ py::PyResult<py::Value> Bytecode::eval_loop(VirtualMachine &vm, Interpreter &int
132132
ASSERT((*vm.instruction_pointer()).get());
133133
const auto &current_ip = vm.instruction_pointer();
134134
const auto &instruction = *current_ip;
135-
spdlog::debug("{} {}", (void *)instruction.get(), instruction->to_string());
135+
// spdlog::debug("{} {}", (void *)instruction.get(), instruction->to_string());
136+
// std::cout << std::format("{} {}", (void *)instruction.get(), instruction->to_string())
137+
// << std::endl;
136138
auto result = instruction->execute(vm, vm.interpreter());
137139
// we left the current stack frame in the previous instruction
138140
if (vm.stack().size() != stack_depth) {

src/executable/bytecode/instructions/ListToTuple.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include "ListToTuple.hpp"
22
#include "runtime/PyList.hpp"
3+
#include "runtime/PyString.hpp"
34
#include "runtime/PyTuple.hpp"
45
#include "vm/VM.hpp"
56

7+
#include <iostream>
8+
69
using namespace py;
710

811
PyResult<Value> ListToTuple::execute(VirtualMachine &vm, Interpreter &) const
@@ -12,6 +15,14 @@ PyResult<Value> ListToTuple::execute(VirtualMachine &vm, Interpreter &) const
1215
ASSERT(std::holds_alternative<PyObject *>(list));
1316

1417
auto *pylist = std::get<PyObject *>(list);
18+
if (!as<PyList>(pylist)) {
19+
std::cout << to_string() << std::endl;
20+
if (!pylist) {
21+
std::cout << "(null)" << std::endl;
22+
} else {
23+
std::cout << pylist->str().unwrap()->to_string() << std::endl;
24+
}
25+
}
1526
ASSERT(as<PyList>(pylist));
1627

1728
auto result = PyTuple::create(as<PyList>(pylist)->elements());

src/executable/bytecode/instructions/YieldFrom.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ PyResult<Value> YieldFrom::execute(VirtualMachine &vm, Interpreter &interpreter)
4949
vm.reg(m_dst) = result.unwrap();
5050
vm.reg(0) = result.unwrap();
5151
vm.set_instruction_pointer(vm.instruction_pointer() - 1);
52-
vm.pop_frame(true);
52+
vm.pop_frame(false);
5353
}
5454

5555
return result;

src/executable/bytecode/instructions/YieldValue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ PyResult<Value> YieldValue::execute(VirtualMachine &vm, Interpreter &interpreter
2323

2424
vm.reg(0) = result;
2525

26-
vm.pop_frame(true);
26+
vm.pop_frame(false);
2727

2828
return Ok(result);
2929
}

src/executable/bytecode/serialization/deserialize.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "serialize.hpp"
66
#include "utilities.hpp"
77

8+
#include <iostream>
89
#include <span>
910
#include <string>
1011

src/executable/mlir/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
find_package(LLVM 20.1 REQUIRED CONFIG)
1+
find_package(LLVM 23 REQUIRED CONFIG)
22
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
33

44
find_package(MLIR CONFIG REQUIRED
@@ -20,6 +20,8 @@ set(PYTHON_MLIR_BINARY_DIR ${PROJECT_BINARY_DIR}/src/executable/mlir)
2020
add_subdirectory(Conversion)
2121
add_subdirectory(Dialect)
2222
add_subdirectory(Target)
23+
add_subdirectory(tools/python-mlir-opt)
24+
add_subdirectory(test)
2325

2426
add_library(python-mlir compile.cpp)
2527
target_link_libraries(python-mlir PRIVATE PythonMLIRDialect TargetPythonBytecode PythonConversionPasses)

src/executable/mlir/Conversion/Passes.td

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,30 @@ include "mlir/Pass/PassBase.td"
44
def ConvertPythonToPythonBytecode : Pass<"convert-python-to-pythonbytecode"> {
55
let summary = "Convert recognized Python ops to PythonCpp bytecode";
66
let constructor = "mlir::py::createPythonToPythonBytecodePass()";
7+
}
8+
9+
def ConvertPyForLoop : Pass<"convert-py-forloop"> {
10+
let summary = "Lower py.for_loop to emitpybytecode control flow";
11+
let constructor = "mlir::py::createConvertForLoopPass()";
12+
}
13+
14+
def ConvertPyWhile : Pass<"convert-py-while"> {
15+
let summary = "Lower py.while to emitpybytecode control flow";
16+
let constructor = "mlir::py::createConvertWhileLoopPass()";
17+
}
18+
19+
def ConvertPyTry : Pass<"convert-py-try"> {
20+
let summary = "Lower py.try (and its handler scopes) to emitpybytecode control flow";
21+
let constructor = "mlir::py::createConvertTryPass()";
22+
}
23+
24+
def ConvertPyWith : Pass<"convert-py-with"> {
25+
let summary = "Lower py.with to emitpybytecode control flow";
26+
let constructor = "mlir::py::createConvertWithPass()";
27+
}
28+
29+
def MaterialiseReturnNone : Pass<"materialise-return-none", "::mlir::func::FuncOp"> {
30+
let summary = "Insert emitpybytecode.LOAD_CONST(None) before zero-operand func.return ops";
31+
let constructor = "mlir::py::createMaterialiseReturnNonePass()";
32+
let dependentDialects = ["::mlir::emitpybytecode::EmitPythonBytecodeDialect"];
733
}

0 commit comments

Comments
 (0)