diff --git a/.github/workflows/premerge.yml b/.github/workflows/premerge.yml index c468d8cc..3a256729 100644 --- a/.github/workflows/premerge.yml +++ b/.github/workflows/premerge.yml @@ -6,10 +6,12 @@ on: env: CMAKE_PRESET: release + CC: gcc-16 + CXX: g++-16 jobs: build: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - uses: actions/checkout@v7 @@ -18,6 +20,16 @@ jobs: - uses: pre-commit/action@v3.0.1 + - name: Install GCC 16 + run: | + # We require C++26, which currently is best supported in gcc-16. + # By default, Ubuntu 24.04 only supports up to gcc-13, + # but ubuntu-toolchain-r/test carries experimental trunk snapshots. + sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test + sudo apt-get update + sudo apt-get install -y gcc-16 g++-16 + g++-16 --version + - name: Install LLVM run: | # LLVM 23 has branched, so it lives in its own apt suite now. Upstream @@ -30,9 +42,7 @@ jobs: echo "deb [signed-by=/etc/apt/keyrings/apt.llvm.org.asc] https://apt.llvm.org/${CODENAME}/ llvm-toolchain-${CODENAME}-23 main" \ | sudo tee /etc/apt/sources.list.d/llvm-23.list > /dev/null sudo apt-get update - # The snapshot packaging pulled in llvm-23-dev via libmlir-23-dev; the - # release packaging does not, and MLIRConfig.cmake needs LLVMConfig.cmake. - sudo apt-get install -y clang-23 lld-23 llvm-23-dev libmlir-23-dev mlir-23-tools + sudo apt-get install -y llvm-23-dev libmlir-23-dev mlir-23-tools - name: ccache uses: hendrikmuhs/ccache-action@v1.2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 6315b183..ff753c6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles) project(python++) -set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD 26) include(cmake/CPM.cmake) @@ -77,10 +77,26 @@ target_compile_options(project_warnings INTERFACE $<$:-Wno-sign-conversion;-Wno-shadow;-Wno-implicit-fallthrough;-Wno-old-style-cast;-Wno-deprecated-copy;-Wno-missing-field-initializers;-Wno-null-dereference;-Wno-maybe-uninitialized;-Wno-stringop-overflow> ) +target_compile_options(project_options + # -fno-omit-frame-pointer is needed, otherwise we cannot access the stack base + # pointer reliably. The garbage collector scans the C++ stack conservatively + # for roots (see MarkSweepGC::collect_roots), so every target whose frames can + # be live across an allocation needs it. + INTERFACE $<$:-fno-omit-frame-pointer> +) + +# Every first-party target links against project_options and project_warnings +# through this helper so they are all compiled the same way. +include(PythonCppFlags) + +# check_cxx_source_compiles links the snippet, so it needs a main(); and +# std::uint64_t needs rather than coming along with . check_cxx_source_compiles( "#include + #include constexpr double f64v = 19880124.0; - constexpr auto u64v = std::bit_cast(f64v);" + constexpr auto u64v = std::bit_cast(f64v); + int main() { return u64v == 0; }" STL_SUPPORTS_BIT_CAST) find_library(MATH_LIBRARY m) diff --git a/CMakePresets.json b/CMakePresets.json index 500e516a..47d05c86 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -70,4 +70,4 @@ } ] -} \ No newline at end of file +} diff --git a/cmake/PythonCppFlags.cmake b/cmake/PythonCppFlags.cmake new file mode 100644 index 00000000..1a905b8f --- /dev/null +++ b/cmake/PythonCppFlags.cmake @@ -0,0 +1,36 @@ +# Helper for giving every first-party target the same compiler flags. +# +# The flags come from the external `project_options` package (added with CPM in +# the top-level CMakeLists.txt), which exposes them as two INTERFACE targets: +# * project_options - sanitizers, hardening, linker and optimisation flags +# * project_warnings - the warning set plus -Werror +# The top-level CMakeLists.txt adjusts both to taste; everything else just links +# against them through `python_cpp_link_project_options()` below. Third-party +# code pulled in by CPM (spdlog, googletest, linenoise, ...) is deliberately +# left alone. +# +# The helper exists because of the LLVM/MLIR target helpers +# (add_mlir_library, add_mlir_conversion_library, add_mlir_translation_library, +# ...): they compile their sources in a separate `obj.` object library +# rather than in `` itself, and only forward include directories to it - +# not the usage requirements of libraries linked afterwards. Linking the flags +# to `` alone would therefore silently compile nothing with them, so this +# always covers the `obj.` twin as well. + +include_guard(GLOBAL) + +function(python_cpp_link_project_options) + foreach(target ${ARGN}) + foreach(name ${target} obj.${target}) + if(NOT TARGET ${name}) + continue() + endif() + get_target_property(type ${name} TYPE) + if(type STREQUAL "INTERFACE_LIBRARY") + target_link_libraries(${name} INTERFACE project_options project_warnings) + else() + target_link_libraries(${name} PRIVATE project_options project_warnings) + endif() + endforeach() + endforeach() +endfunction() diff --git a/integration/CMakeLists.txt b/integration/CMakeLists.txt index 4c2d33b9..3dea8b95 100644 --- a/integration/CMakeLists.txt +++ b/integration/CMakeLists.txt @@ -1,5 +1,6 @@ add_executable(integration-tests_ program.cpp ../src/testing/main.cpp) -target_link_libraries(integration-tests_ PRIVATE python-cpp gtest gtest_main cxxopts project_options project_warnings tsl::ordered_map) +target_link_libraries(integration-tests_ PRIVATE python-cpp gtest gtest_main cxxopts tsl::ordered_map) +python_cpp_link_project_options(integration-tests_) # gtest_discover_tests(integration-tests_) add_test( diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2afd2319..7598e9ed 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -269,8 +269,6 @@ add_executable(unittests_ ${UNITTEST_SOURCES}) target_link_libraries(python-cpp PUBLIC spdlog m PRIVATE - project_options - project_warnings ICU::uc ICU::data ${GMPXX_LIBRARIES} @@ -283,11 +281,7 @@ target_include_directories(python-cpp PRIVATE ${GMP_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ) -target_compile_options( - python-cpp - PRIVATE - # -fno-omit-frame-pointer is needed, otherwise we cannot access the stack base pointer reliably - -fno-omit-frame-pointer) +python_cpp_link_project_options(python-cpp) if(STL_SUPPORTS_BIT_CAST) target_compile_definitions(python-cpp PUBLIC "STL_SUPPORTS_BIT_CAST") @@ -312,14 +306,6 @@ elseif(ENABLE_LLVM_BACKEND AND LLVM_FOUND) message(STATUS "Configuring LLVM backend") add_library(python-cpp-llvm ${LLVM_BACKEND_FILES}) - target_compile_options( - python-cpp-llvm - PRIVATE -Wall - -Wextra - -Werror - -Wno-unused-parameter - -fno-omit-frame-pointer) - add_library(llvm-interface INTERFACE) target_include_directories(llvm-interface INTERFACE . ) # include llvm include directories as system paths to silence compiler warnings @@ -337,7 +323,8 @@ elseif(ENABLE_LLVM_BACKEND AND LLVM_FOUND) orcjit x86asmparser x86codegen) - target_link_libraries(llvm-interface INTERFACE ${llvm_libs} project_options project_warnings) + target_link_libraries(llvm-interface INTERFACE ${llvm_libs}) + python_cpp_link_project_options(llvm-interface) # TODO: not all versions of llvm are ready for C++20, figure out when to use this set_property(TARGET python-cpp-llvm PROPERTY CXX_STANDARD 17) @@ -355,12 +342,14 @@ elseif(ENABLE_LLVM_BACKEND AND LLVM_FOUND) target_link_libraries(unittests_ PRIVATE python-cpp-llvm) endif() -target_link_libraries(unittests_ PRIVATE python-cpp gtest gtest_main cxxopts project_options project_warnings tsl::ordered_map) +target_link_libraries(unittests_ PRIVATE python-cpp gtest gtest_main cxxopts tsl::ordered_map) gtest_discover_tests(unittests_) add_executable(python repl/repl.cpp) -target_link_libraries(python PRIVATE linenoise cxxopts python-cpp project_options project_warnings stdc++) +target_link_libraries(python PRIVATE linenoise cxxopts python-cpp stdc++) add_executable(freeze utilities/freeze.cpp) -target_link_libraries(freeze PRIVATE python-cpp cxxopts project_options project_warnings) +target_link_libraries(freeze PRIVATE python-cpp cxxopts) target_include_directories(freeze SYSTEM PRIVATE ${MLIR_INCLUDE_DIRS}) + +python_cpp_link_project_options(unittests_ python freeze) diff --git a/src/executable/bytecode/instructions/Instructions.cpp b/src/executable/bytecode/instructions/Instructions.cpp index c7c89830..9a619b34 100644 --- a/src/executable/bytecode/instructions/Instructions.cpp +++ b/src/executable/bytecode/instructions/Instructions.cpp @@ -79,10 +79,20 @@ using namespace py; +// GCC 16 inlines the std::variant copy-assignment behind py::Value and then +// attributes the std::vector destructor's operator delete to the stack slot +// holding the variant. Nothing is freed here; the diagnostic is a false positive. +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wfree-nonheap-object" +#endif Instruction::RAIIStoreNonCallInstructionData::RAIIStoreNonCallInstructionData() { reg0 = VirtualMachine::the().reg(0); } +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif Instruction::RAIIStoreNonCallInstructionData::~RAIIStoreNonCallInstructionData() { diff --git a/src/executable/mlir/CMakeLists.txt b/src/executable/mlir/CMakeLists.txt index 8e633477..3e3c023f 100644 --- a/src/executable/mlir/CMakeLists.txt +++ b/src/executable/mlir/CMakeLists.txt @@ -27,6 +27,23 @@ include(AddLLVM) set(PYTHON_MLIR_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src/executable/mlir) set(PYTHON_MLIR_BINARY_DIR ${PROJECT_BINARY_DIR}/src/executable/mlir) +# The LLVM/MLIR helpers used below (add_mlir_library and friends) compile their +# sources in a separate `obj.` object library and forward the target's +# INCLUDE_DIRECTORIES to it as plain include paths, which drops the SYSTEM +# marking. Marking the third-party headers as system directories for the whole +# subtree survives that, and keeps our warning set (-Werror included) from +# firing inside LLVM, MLIR and spdlog headers. +# +# ${PYTHON_MLIR_BINARY_DIR} holds nothing but TableGen output (Ops.h.inc, +# Passes.h.inc, ...), which is machine-generated and not ours to clean up, so it +# is treated the same way. +include_directories(SYSTEM + ${LLVM_INCLUDE_DIRS} + ${MLIR_INCLUDE_DIRS} + ${spdlog_SOURCE_DIR}/include + ${PYTHON_MLIR_BINARY_DIR} + ${PYTHON_MLIR_BINARY_DIR}/Dialect) + add_subdirectory(Conversion) add_subdirectory(Dialect) add_subdirectory(Target) @@ -35,3 +52,4 @@ add_subdirectory(test) add_library(python-mlir compile.cpp) target_link_libraries(python-mlir PRIVATE PythonMLIRDialect TargetPythonBytecode PythonConversionPasses) +python_cpp_link_project_options(python-mlir) diff --git a/src/executable/mlir/Conversion/CMakeLists.txt b/src/executable/mlir/Conversion/CMakeLists.txt index 2014b6f3..fbc1c130 100644 --- a/src/executable/mlir/Conversion/CMakeLists.txt +++ b/src/executable/mlir/Conversion/CMakeLists.txt @@ -15,4 +15,6 @@ add_mlir_library(PythonConversionPasses LINK_LIBS PUBLIC ${PYTHON_CONVERSION_LIBS} -) \ No newline at end of file +) + +python_cpp_link_project_options(PythonConversionPasses) \ No newline at end of file diff --git a/src/executable/mlir/Conversion/PythonToPythonBytecode/CMakeLists.txt b/src/executable/mlir/Conversion/PythonToPythonBytecode/CMakeLists.txt index 75b9be68..1e07fdc3 100644 --- a/src/executable/mlir/Conversion/PythonToPythonBytecode/CMakeLists.txt +++ b/src/executable/mlir/Conversion/PythonToPythonBytecode/CMakeLists.txt @@ -27,4 +27,5 @@ target_include_directories(PythonToPythonBytecode PUBLIC ${PYTHON_MLIR_BINARY_DIR} ) -target_link_libraries(PythonToPythonBytecode PRIVATE spdlog) \ No newline at end of file +target_link_libraries(PythonToPythonBytecode PRIVATE spdlog) +python_cpp_link_project_options(PythonToPythonBytecode) \ No newline at end of file diff --git a/src/executable/mlir/Conversion/PythonToPythonBytecode/CollectionPatterns.cpp b/src/executable/mlir/Conversion/PythonToPythonBytecode/CollectionPatterns.cpp index 5bd4cd11..cfd6e6d0 100644 --- a/src/executable/mlir/Conversion/PythonToPythonBytecode/CollectionPatterns.cpp +++ b/src/executable/mlir/Conversion/PythonToPythonBytecode/CollectionPatterns.cpp @@ -36,13 +36,13 @@ namespace { llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) { if (to_expand) { if (!result.has_value()) { - result = rewriter.create( - op.getLoc(), op.getOutput().getType(), keys, values); + result = mlir::emitpybytecode::BuildDict::create( + rewriter, op.getLoc(), op.getOutput().getType(), keys, values); keys.clear(); values.clear(); } - rewriter.create( - op.getLoc(), *result, value); + mlir::emitpybytecode::DictUpdate::create( + rewriter, op.getLoc(), *result, value); } else { if (!result.has_value()) { keys.push_back(key); @@ -50,8 +50,8 @@ namespace { } else { ASSERT(keys.empty()); ASSERT(values.empty()); - rewriter.create( - op.getLoc(), *result, key, value); + mlir::emitpybytecode::DictAdd::create( + rewriter, op.getLoc(), *result, key, value); } } } @@ -89,12 +89,12 @@ namespace { llvm::ArrayRef requires_expansion) { auto list = - rewriter.create(loc, list_type, mlir::ValueRange{}); + mlir::emitpybytecode::BuildList::create(rewriter, loc, list_type, mlir::ValueRange{}); for (auto [el, expand] : llvm::zip(elements, requires_expansion)) { if (expand) { - rewriter.create(loc, list, el); + mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el); } else { - rewriter.create(loc, list, el); + mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el); } } return list; @@ -181,23 +181,23 @@ namespace { for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) { if (expand) { if (!set.has_value()) { - set = rewriter.create( - op->getLoc(), op.getOutput().getType(), elements); + set = mlir::emitpybytecode::BuildSet::create( + rewriter, op->getLoc(), op.getOutput().getType(), elements); } else { for (auto el : elements) { - rewriter.create( - op.getLoc(), *set, el); + mlir::emitpybytecode::SetAdd::create( + rewriter, op.getLoc(), *set, el); } } elements.clear(); - rewriter.create(op.getLoc(), *set, el); + mlir::emitpybytecode::SetUpdate::create(rewriter, op.getLoc(), *set, el); } else { elements.push_back(el); } } ASSERT(set.has_value()); for (auto el : elements) { - rewriter.create(op.getLoc(), *set, el); + mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el); } rewriter.replaceOp(op, *set); } else { diff --git a/src/executable/mlir/Conversion/PythonToPythonBytecode/ControlFlowPatterns.cpp b/src/executable/mlir/Conversion/PythonToPythonBytecode/ControlFlowPatterns.cpp index 65243cde..f2262c50 100644 --- a/src/executable/mlir/Conversion/PythonToPythonBytecode/ControlFlowPatterns.cpp +++ b/src/executable/mlir/Conversion/PythonToPythonBytecode/ControlFlowPatterns.cpp @@ -97,8 +97,8 @@ namespace { auto insertion_point = rewriter.getInsertionPoint(); auto *return_block = rewriter.createBlock(&op.getRegion()); auto value = - rewriter.create(op.getLoc(), rewriter.getNoneType()); - rewriter.create(op.getLoc(), mlir::ValueRange{ value }); + mlir::py::ConstantOp::create(rewriter, op.getLoc(), rewriter.getNoneType()); + mlir::func::ReturnOp::create(rewriter, op.getLoc(), mlir::ValueRange{ value }); rewriter.setInsertionPoint(insertion_point->getBlock(), insertion_point); return return_block; }) @@ -136,9 +136,10 @@ namespace { mlir::LogicalResult matchAndRewrite(mlir::py::YieldFromOp op, mlir::PatternRewriter &rewriter) const final { - auto iterator = rewriter.create( - op.getLoc(), op.getIterable().getType(), op.getIterable()); - auto value = rewriter.create(op.getLoc(), rewriter.getNoneType()); + auto iterator = mlir::emitpybytecode::YieldFromIter::create( + rewriter, op.getLoc(), op.getIterable().getType(), op.getIterable()); + auto value = + mlir::py::ConstantOp::create(rewriter, op.getLoc(), rewriter.getNoneType()); rewriter.replaceOpWithNewOp( op, iterator.getType(), iterator, value); diff --git a/src/executable/mlir/Conversion/PythonToPythonBytecode/FunctionPatterns.cpp b/src/executable/mlir/Conversion/PythonToPythonBytecode/FunctionPatterns.cpp index 232fc2a1..30bb80bf 100644 --- a/src/executable/mlir/Conversion/PythonToPythonBytecode/FunctionPatterns.cpp +++ b/src/executable/mlir/Conversion/PythonToPythonBytecode/FunctionPatterns.cpp @@ -77,7 +77,8 @@ namespace { ASSERT(function_definition); ASSERT(mlir::isa(*function_definition)); - auto sym_name = rewriter.create(op.getLoc(), + auto sym_name = mlir::emitpybytecode::ConstantOp::create(rewriter, + op.getLoc(), mlir::py::PyObjectType::get(rewriter.getContext()), rewriter.getStringAttr(op.getFunctionName())); @@ -86,11 +87,11 @@ namespace { std::vector captures_vec; for (auto attr : op.getCaptures()) { auto name = mlir::cast(attr).getValue(); - captures_vec.push_back(rewriter.create( - op.getLoc(), mlir::py::PyObjectType::get(getContext()), name)); + captures_vec.push_back(mlir::emitpybytecode::LoadClosureOp::create( + rewriter, op.getLoc(), mlir::py::PyObjectType::get(getContext()), name)); } - return rewriter.create( - op.getLoc(), mlir::py::PyObjectType::get(getContext()), captures_vec); + return mlir::emitpybytecode::BuildTuple::create( + rewriter, op.getLoc(), mlir::py::PyObjectType::get(getContext()), captures_vec); }(); rewriter.replaceOpWithNewOp(op, mlir::py::PyObjectType::get(rewriter.getContext()), @@ -117,7 +118,7 @@ namespace { void populate_arguments(mlir::func::FuncOp &op, mlir::OpBuilder &builder) const { - for (size_t i = 0; i < op.getNumArguments(); ++i) { + for (unsigned i = 0; i < op.getNumArguments(); ++i) { auto arg_name = op.getArgAttr(i, "llvm.name"); ASSERT(arg_name); detail::add_identifier_to( @@ -155,7 +156,8 @@ namespace { auto func_type = rewriter.getFunctionType(mlir::TypeRange{}, mlir::TypeRange{ mlir::py::PyObjectType::get(rewriter.getContext()) }); - auto class_fn_definition = rewriter.create(op.getLoc(), + auto class_fn_definition = mlir::func::FuncOp::create(rewriter, + op.getLoc(), op.getMangledName(), func_type, mlir::ArrayRef{}, @@ -205,7 +207,8 @@ namespace { rewriter.eraseBlock(end); rewriter.setInsertionPoint(op); - auto class_name = rewriter.create(op.getLoc(), + auto class_name = mlir::emitpybytecode::ConstantOp::create(rewriter, + op.getLoc(), mlir::py::PyObjectType::get(rewriter.getContext()), rewriter.getStringAttr(op.getMangledName())); @@ -214,22 +217,23 @@ namespace { std::vector captures_vec; for (auto attr : op.getCaptures()) { auto name = mlir::cast(attr).getValue(); - captures_vec.push_back(rewriter.create( - op.getLoc(), mlir::py::PyObjectType::get(getContext()), name)); + captures_vec.push_back(mlir::emitpybytecode::LoadClosureOp::create( + rewriter, op.getLoc(), mlir::py::PyObjectType::get(getContext()), name)); } - return rewriter.create( - op.getLoc(), mlir::py::PyObjectType::get(getContext()), captures_vec); + return mlir::emitpybytecode::BuildTuple::create( + rewriter, op.getLoc(), mlir::py::PyObjectType::get(getContext()), captures_vec); }(); - auto class_fn = rewriter.create(op.getLoc(), + auto class_fn = mlir::emitpybytecode::MakeFunction::create(rewriter, + op.getLoc(), mlir::py::PyObjectType::get(rewriter.getContext()), class_name, mlir::ValueRange{}, mlir::ValueRange{}, captures_tuple); - auto class_builder = rewriter.create( - op.getLoc(), mlir::py::PyObjectType::get(rewriter.getContext())); + auto class_builder = mlir::emitpybytecode::LoadBuildClass::create( + rewriter, op.getLoc(), mlir::py::PyObjectType::get(rewriter.getContext())); std::vector args{ class_fn, class_name }; args.insert(args.end(), op.getBases().begin(), op.getBases().end()); rewriter.replaceOpWithNewOp(op, diff --git a/src/executable/mlir/Conversion/PythonToPythonBytecode/ImportPatterns.cpp b/src/executable/mlir/Conversion/PythonToPythonBytecode/ImportPatterns.cpp index 05194168..6f703919 100644 --- a/src/executable/mlir/Conversion/PythonToPythonBytecode/ImportPatterns.cpp +++ b/src/executable/mlir/Conversion/PythonToPythonBytecode/ImportPatterns.cpp @@ -25,16 +25,18 @@ namespace { mlir::PatternRewriter &rewriter) const final { auto name = op.getName(); - auto level = rewriter.create( - op.getLoc(), op.getModule().getType(), rewriter.getUI32IntegerAttr(op.getLevel())); + auto level = mlir::emitpybytecode::ConstantOp::create(rewriter, + op.getLoc(), + op.getModule().getType(), + rewriter.getUI32IntegerAttr(op.getLevel())); std::vector els; for (auto attr : op.getFromList()) { auto from = mlir::cast(attr); - els.push_back(rewriter.create( - op.getLoc(), op.getModule().getType(), from)); + els.push_back(mlir::emitpybytecode::ConstantOp::create( + rewriter, op.getLoc(), op.getModule().getType(), from)); } - auto from_list = rewriter.create( - op.getLoc(), op.getModule().getType(), els); + auto from_list = mlir::emitpybytecode::BuildTuple::create( + rewriter, op.getLoc(), op.getModule().getType(), els); rewriter.replaceOpWithNewOp( op, op.getModule().getType(), name, level, from_list); diff --git a/src/executable/mlir/Conversion/PythonToPythonBytecode/PythonToPythonBytecode.cpp b/src/executable/mlir/Conversion/PythonToPythonBytecode/PythonToPythonBytecode.cpp index 8354be03..c7eaab95 100644 --- a/src/executable/mlir/Conversion/PythonToPythonBytecode/PythonToPythonBytecode.cpp +++ b/src/executable/mlir/Conversion/PythonToPythonBytecode/PythonToPythonBytecode.cpp @@ -144,12 +144,13 @@ namespace py { mlir::py::BranchYieldOp yield_op) { if (finally_exits.empty()) { - rewriter.create(yield_op.getLoc(), yield_op.getKindAttr()); + mlir::py::BranchYieldOp::create( + rewriter, yield_op.getLoc(), yield_op.getKindAttr()); return; } auto it = finally_exits.find(static_cast(*yield_op.getKind())); ASSERT(it != finally_exits.end()); - rewriter.create(yield_op.getLoc(), it->second); + mlir::cf::BranchOp::create(rewriter, yield_op.getLoc(), it->second); } // For each break/continue kind that escapes the try through its finally, @@ -205,17 +206,18 @@ namespace py { auto iterable = op.getIterable(); rewriter.setInsertionPointToEnd(initBlock); - auto iterator = rewriter.create( - op.getStep().getLoc(), iterable.getType(), iterable); + auto iterator = mlir::emitpybytecode::GetIter::create( + rewriter, op.getStep().getLoc(), iterable.getType(), iterable); // advance iterator auto iterator_next_block = rewriter.createBlock(endBlock); rewriter.setInsertionPointToEnd(initBlock); - rewriter.create(op.getStep().getLoc(), iterator_next_block); + mlir::cf::BranchOp::create(rewriter, op.getStep().getLoc(), iterator_next_block); rewriter.setInsertionPointToStart(iterator_next_block); - rewriter.create(op.getStep().getLoc(), + mlir::emitpybytecode::ForIter::create(rewriter, + op.getStep().getLoc(), iterator, &op.getStep().front(), op.getOrelse().empty() ? endBlock : &op.getOrelse().front()); @@ -230,7 +232,8 @@ namespace py { iterator_exit_block->getTerminator(), &op.getBody().front()); auto *for_iter_block = rewriter.createBlock(&op.getBody()); - rewriter.create(op.getStep().getLoc(), + mlir::emitpybytecode::ForIter::create(rewriter, + op.getStep().getLoc(), iterator, &op.getStep().front(), op.getOrelse().empty() ? endBlock : &op.getOrelse().front()); @@ -296,17 +299,18 @@ namespace py { ASSERT(condition_op); rewriter.setInsertionPointToEnd(initBlock); - rewriter.create(condition_op.getLoc(), &condition_start); + mlir::cf::BranchOp::create(rewriter, condition_op.getLoc(), &condition_start); if (mlir::isa(condition_op.getCond())) { rewriter.setInsertionPointToStart(condition_op.getCond().getParentBlock()); } else { rewriter.setInsertionPointAfter(condition_op.getCond().getDefiningOp()); } - auto should_jump = rewriter.create( - condition_op.getLoc(), rewriter.getI1Type(), condition_op.getCond()); + auto should_jump = mlir::py::CastToBoolOp::create( + rewriter, condition_op.getLoc(), rewriter.getI1Type(), condition_op.getCond()); ASSERT(!op.getBody().empty()); - rewriter.create(condition_op.getLoc(), + mlir::cf::CondBranchOp::create(rewriter, + condition_op.getLoc(), should_jump, &op.getBody().front(), op.getOrelse().empty() ? endBlock : &op.getOrelse().front()); @@ -412,8 +416,8 @@ namespace py { auto *current = childOp->getBlock(); auto *next = rewriter.splitBlock(current, childOp->getIterator()); rewriter.setInsertionPointToEnd(current); - rewriter.create( - childOp->getLoc()); + mlir::emitpybytecode::LeaveExceptionHandle::create( + rewriter, childOp->getLoc()); if (auto y = mlir::cast(childOp); y.getKind().has_value()) { // break/continue out of the try body: pop the @@ -425,16 +429,16 @@ namespace py { } if (op.getHandlers().empty()) { ASSERT(!op.getFinally().empty()); - rewriter.create( - childOp->getLoc(), &op.getFinally().front()); + mlir::cf::BranchOp::create( + rewriter, childOp->getLoc(), &op.getFinally().front()); } else if (!op.getOrelse().empty()) { - rewriter.create( - childOp->getLoc(), &op.getOrelse().front()); + mlir::cf::BranchOp::create( + rewriter, childOp->getLoc(), &op.getOrelse().front()); } else if (!op.getFinally().empty()) { - rewriter.create( - childOp->getLoc(), &op.getFinally().front()); + mlir::cf::BranchOp::create( + rewriter, childOp->getLoc(), &op.getFinally().front()); } else { - rewriter.create(childOp->getLoc(), endBlock); + mlir::cf::BranchOp::create(rewriter, childOp->getLoc(), endBlock); } rewriter.eraseBlock(next); }); @@ -464,11 +468,11 @@ namespace py { auto *next = rewriter.splitBlock(current, childOp->getIterator()); rewriter.setInsertionPointToEnd(current); if (kind_attr) { - rewriter.create( - childOp->getLoc(), kind_attr); + mlir::py::BranchYieldOp::create( + rewriter, childOp->getLoc(), kind_attr); } else { - rewriter.create( - childOp->getLoc(), endBlock); + mlir::cf::BranchOp::create( + rewriter, childOp->getLoc(), endBlock); } rewriter.eraseBlock(next); } @@ -484,13 +488,13 @@ namespace py { auto *next = rewriter.splitBlock(current, childOp->getIterator()); rewriter.setInsertionPointToEnd(current); if (kind_attr) { - rewriter.create( - childOp->getLoc()); - rewriter.create( - childOp->getLoc(), kind_attr); + mlir::emitpybytecode::ClearExceptionState::create( + rewriter, childOp->getLoc()); + mlir::py::BranchYieldOp::create( + rewriter, childOp->getLoc(), kind_attr); } else { - rewriter.create( - childOp->getLoc(), endBlock); + mlir::emitpybytecode::ReRaiseOp::create( + rewriter, childOp->getLoc(), endBlock); } rewriter.eraseBlock(next); } @@ -504,14 +508,17 @@ namespace py { auto handler_scope = mlir::cast(handler.front().getTerminator()); ASSERT(handler_scope); - rewriter.create(op.getLoc(), + mlir::emitpybytecode::SetupExceptionHandle::create(rewriter, + op.getLoc(), body_start, handler_scope.getCond().empty() ? &handler_scope.getHandler().front() : &handler_scope.getCond().front()); } else { ASSERT(finally_mapping.has_value()); - rewriter.create( - op.getLoc(), body_start, finally_mapping->lookup(&op.getFinally().front())); + mlir::emitpybytecode::SetupExceptionHandle::create(rewriter, + op.getLoc(), + body_start, + finally_mapping->lookup(&op.getFinally().front())); } if (!op.getHandlers().empty()) { @@ -550,8 +557,8 @@ namespace py { auto *current = childOp->getBlock(); auto *next = rewriter.splitBlock(current, childOp->getIterator()); rewriter.setInsertionPointToEnd(current); - rewriter.create( - op.getLoc()); + mlir::emitpybytecode::ClearExceptionState::create( + rewriter, op.getLoc()); if (auto y = mlir::cast(childOp); y.getKind().has_value()) { // break/continue out of an except handler: @@ -562,11 +569,11 @@ namespace py { return; } if (!op.getFinally().empty()) { - rewriter.create( - childOp->getLoc(), &op.getFinally().front()); + mlir::cf::BranchOp::create( + rewriter, childOp->getLoc(), &op.getFinally().front()); } else { - rewriter.create( - childOp->getLoc(), endBlock); + mlir::cf::BranchOp::create( + rewriter, childOp->getLoc(), endBlock); } rewriter.eraseBlock(next); }); @@ -585,7 +592,7 @@ namespace py { ASSERT(cond); auto *reraise_block = rewriter.createBlock(&handler_scope.getCond()); - rewriter.create(cond.getLoc()); + mlir::py::RaiseOp::create(rewriter, cond.getLoc()); rewriter.setInsertionPoint(cond); rewriter.replaceOpWithNewOp(cond, @@ -605,8 +612,8 @@ namespace py { auto *current = childOp->getBlock(); auto *next = rewriter.splitBlock(current, childOp->getIterator()); rewriter.setInsertionPointToEnd(current); - rewriter.create( - op.getLoc()); + mlir::emitpybytecode::ClearExceptionState::create( + rewriter, op.getLoc()); if (auto y = mlir::cast(childOp); y.getKind().has_value()) { // break/continue out of an except handler: @@ -617,11 +624,11 @@ namespace py { return; } if (!op.getFinally().empty()) { - rewriter.create( - childOp->getLoc(), &op.getFinally().front()); + mlir::cf::BranchOp::create( + rewriter, childOp->getLoc(), &op.getFinally().front()); } else { - rewriter.create( - childOp->getLoc(), endBlock); + mlir::cf::BranchOp::create( + rewriter, childOp->getLoc(), endBlock); } rewriter.eraseBlock(next); }); @@ -644,10 +651,10 @@ namespace py { return; } if (!op.getFinally().empty()) { - rewriter.create( - childOp->getLoc(), &op.getFinally().front()); + mlir::cf::BranchOp::create( + rewriter, childOp->getLoc(), &op.getFinally().front()); } else { - rewriter.create(childOp->getLoc(), endBlock); + mlir::cf::BranchOp::create(rewriter, childOp->getLoc(), endBlock); } rewriter.eraseBlock(next); }); @@ -682,13 +689,15 @@ namespace py { // and the break/continue path (both leave without an exception). auto emit_normal_exit = [&rewriter, &op]() { for (const auto &item : op.getItems()) { - auto exit = rewriter.create(item.getLoc(), + auto exit = mlir::py::LoadMethodOp::create(rewriter, + item.getLoc(), mlir::py::PyObjectType::get(rewriter.getContext()), item, "__exit__"); - auto none = rewriter.create( - item.getLoc(), rewriter.getNoneType()); - rewriter.create(item.getLoc(), + auto none = mlir::py::ConstantOp::create( + rewriter, item.getLoc(), rewriter.getNoneType()); + mlir::py::FunctionCallOp::create(rewriter, + item.getLoc(), mlir::py::PyObjectType::get(rewriter.getContext()), exit, std::vector{ none, none, none }, @@ -699,7 +708,7 @@ namespace py { std::vector{}, false, false); - rewriter.create(item.getLoc()); + mlir::py::ClearExceptionStateOp::create(rewriter, item.getLoc()); } }; @@ -735,8 +744,8 @@ namespace py { auto *current = y->getBlock(); auto *next = rewriter.splitBlock(current, y->getIterator()); rewriter.setInsertionPointToEnd(current); - rewriter.create(y->getLoc()); - rewriter.create(y->getLoc(), exit_block); + mlir::emitpybytecode::LeaveExceptionHandle::create(rewriter, y->getLoc()); + mlir::cf::BranchOp::create(rewriter, y->getLoc(), exit_block); rewriter.eraseBlock(next); } else if (auto y = mlir::dyn_cast(childOp); y && y.getKind().has_value()) { @@ -747,11 +756,11 @@ namespace py { auto *next = rewriter.splitBlock(current, y->getIterator()); auto *lc_block = rewriter.createBlock(endBlock); rewriter.setInsertionPointToEnd(current); - rewriter.create(y->getLoc()); - rewriter.create(y->getLoc(), lc_block); + mlir::emitpybytecode::LeaveExceptionHandle::create(rewriter, y->getLoc()); + mlir::cf::BranchOp::create(rewriter, y->getLoc(), lc_block); rewriter.setInsertionPointToStart(lc_block); emit_normal_exit(); - rewriter.create(y->getLoc(), y.getKindAttr()); + mlir::py::BranchYieldOp::create(rewriter, y->getLoc(), y.getKindAttr()); rewriter.eraseBlock(next); } return WalkResult::advance(); @@ -770,38 +779,41 @@ namespace py { && "WithOp lowering does not yet support multiple context managers"); rewriter.setInsertionPointToStart(cleanup_block); for (const auto &item : op.getItems()) { - auto exit = rewriter.create(item.getLoc(), + auto exit = mlir::py::LoadMethodOp::create(rewriter, + item.getLoc(), mlir::py::PyObjectType::get(rewriter.getContext()), item, "__exit__"); - auto except_result = rewriter.create( - item.getLoc(), mlir::py::PyObjectType::get(rewriter.getContext()), exit); + auto except_result = mlir::py::WithExceptStartOp::create(rewriter, + item.getLoc(), + mlir::py::PyObjectType::get(rewriter.getContext()), + exit); auto *reraise_block = rewriter.createBlock(endBlock); auto *continue_block = rewriter.createBlock(endBlock); rewriter.setInsertionPointAfter(except_result); - auto cond = rewriter.create( - except_result.getLoc(), rewriter.getI1Type(), except_result); - rewriter.create( - cond.getLoc(), cond, continue_block, reraise_block); + auto cond = mlir::py::CastToBoolOp::create( + rewriter, except_result.getLoc(), rewriter.getI1Type(), except_result); + mlir::cf::CondBranchOp::create( + rewriter, cond.getLoc(), cond, continue_block, reraise_block); rewriter.setInsertionPointToStart(reraise_block); - rewriter.create(item.getLoc(), endBlock); + mlir::emitpybytecode::ReRaiseOp::create(rewriter, item.getLoc(), endBlock); rewriter.setInsertionPointToStart(continue_block); - rewriter.create(item.getLoc()); - rewriter.create(op.getLoc(), endBlock); + mlir::emitpybytecode::ClearExceptionState::create(rewriter, item.getLoc()); + mlir::cf::BranchOp::create(rewriter, op.getLoc(), endBlock); } rewriter.setInsertionPointToStart(exit_block); emit_normal_exit(); - rewriter.create(op.getLoc(), endBlock); + mlir::cf::BranchOp::create(rewriter, op.getLoc(), endBlock); rewriter.setInsertionPointToEnd(initBlock); - rewriter.create( - op.getLoc(), body_start, cleanup_block); + mlir::emitpybytecode::SetupWith::create( + rewriter, op.getLoc(), body_start, cleanup_block); rewriter.eraseOp(op); @@ -913,8 +925,8 @@ namespace py { if (!parent) { return mlir::failure(); } auto pyobject_ty = mlir::py::PyObjectType::get(rewriter.getContext()); rewriter.setInsertionPoint(op); - auto none = rewriter.create( - op.getLoc(), pyobject_ty, rewriter.getUnitAttr()); + auto none = mlir::emitpybytecode::ConstantOp::create( + rewriter, op.getLoc(), pyobject_ty, rewriter.getUnitAttr()); rewriter.replaceOpWithNewOp(op, mlir::ValueRange{ none }); // Restore the function signature if RemoveDeadValues stripped diff --git a/src/executable/mlir/Dialect/EmitPythonBytecode/IR/CMakeLists.txt b/src/executable/mlir/Dialect/EmitPythonBytecode/IR/CMakeLists.txt index 9458c48c..d469b48e 100644 --- a/src/executable/mlir/Dialect/EmitPythonBytecode/IR/CMakeLists.txt +++ b/src/executable/mlir/Dialect/EmitPythonBytecode/IR/CMakeLists.txt @@ -17,6 +17,6 @@ add_mlir_library(EmitPythonBytecodeDialect ) target_include_directories(EmitPythonBytecodeDialect PRIVATE - ${MLIR_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/src/executable/mlir/Dialect ${CMAKE_BINARY_DIR}/src/executable/mlir/Dialect) +python_cpp_link_project_options(EmitPythonBytecodeDialect) diff --git a/src/executable/mlir/Dialect/EmitPythonBytecode/IR/EmitPythonBytecode.cpp b/src/executable/mlir/Dialect/EmitPythonBytecode/IR/EmitPythonBytecode.cpp index b4d3f8c1..17803341 100644 --- a/src/executable/mlir/Dialect/EmitPythonBytecode/IR/EmitPythonBytecode.cpp +++ b/src/executable/mlir/Dialect/EmitPythonBytecode/IR/EmitPythonBytecode.cpp @@ -68,12 +68,15 @@ namespace emitpybytecode { auto keys = op.getKeys(); auto values = op.getValues(); rewriter.setInsertionPointAfterValue(keys.front()); - auto result = rewriter.create( - op->getLoc(), op.getOutput().getType(), mlir::ValueRange{}, mlir::ValueRange{}); + auto result = BuildDict::create(rewriter, + op->getLoc(), + op.getOutput().getType(), + mlir::ValueRange{}, + mlir::ValueRange{}); for (auto [key, value] : llvm::zip(keys, values)) { rewriter.setInsertionPointAfterValue(value); - rewriter.create(op.getLoc(), result, key, value); + DictAdd::create(rewriter, op.getLoc(), result, key, value); } rewriter.replaceOp(op, result); return mlir::success(); @@ -112,10 +115,10 @@ namespace emitpybytecode { } auto loc = op.getLoc(); auto output_type = op.getOutput().getType(); - auto list = rewriter.create(loc, output_type, mlir::ValueRange{}); - auto tuple = rewriter.create( - loc, output_type, mlir::ArrayAttr::get(getContext(), elements)); - rewriter.create(loc, list, tuple); + auto list = BuildList::create(rewriter, loc, output_type, mlir::ValueRange{}); + auto tuple = ConstantOp::create( + rewriter, loc, output_type, mlir::ArrayAttr::get(getContext(), elements)); + ListExtend::create(rewriter, loc, list, tuple); rewriter.replaceOp(op, list); return mlir::success(); } diff --git a/src/executable/mlir/Dialect/Python/CMakeLists.txt b/src/executable/mlir/Dialect/Python/CMakeLists.txt index 35a2f794..f5834565 100644 --- a/src/executable/mlir/Dialect/Python/CMakeLists.txt +++ b/src/executable/mlir/Dialect/Python/CMakeLists.txt @@ -20,15 +20,16 @@ add_mlir_library(PythonMLIRDialect MLIRTransformUtils ) +# The MLIR headers come from the subtree-wide include_directories(SYSTEM ...) +# set up in src/executable/mlir/CMakeLists.txt. target_include_directories(PythonMLIRDialect PUBLIC ${PROJECT_SOURCE_DIR}/src - ${MLIR_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/src/executable/mlir/Dialect ${CMAKE_BINARY_DIR}/src/executable/mlir/Dialect) target_link_libraries(PythonMLIRDialect PRIVATE - project_options ${GMPXX_LIBRARIES} ${GMP_LIBRARIES} PUBLIC - spdlog) \ No newline at end of file + spdlog) +python_cpp_link_project_options(PythonMLIRDialect) \ No newline at end of file diff --git a/src/executable/mlir/Dialect/Python/IR/Ops.cpp b/src/executable/mlir/Dialect/Python/IR/Ops.cpp index 4f122e45..31a7e8c1 100644 --- a/src/executable/mlir/Dialect/Python/IR/Ops.cpp +++ b/src/executable/mlir/Dialect/Python/IR/Ops.cpp @@ -169,7 +169,7 @@ namespace py { if (getRequiresArgsExpansion() || getRequiresKwargsExpansion()) { return mlir::success(); } const auto keywords_size = getKeywords().size(); const auto kwargs_size = getKwargs().size(); - if (keywords_size != kwargs_size) { + if (static_cast(keywords_size) != kwargs_size) { return emitOpError() << "has " << keywords_size << " keyword name(s) but " << kwargs_size << " kwargs value(s)"; } @@ -196,7 +196,7 @@ namespace py { // parallel rule always applies. const auto keywords_size = getKeywords().size(); const auto kwargs_size = getKwargs().size(); - if (keywords_size != kwargs_size) { + if (static_cast(keywords_size) != kwargs_size) { return emitOpError() << "has " << keywords_size << " keyword name(s) but " << kwargs_size << " kwargs value(s)"; } @@ -373,7 +373,7 @@ namespace py { } } - void BranchYieldOp::getSuccessorRegions(llvm::ArrayRef operands, + void BranchYieldOp::getSuccessorRegions(llvm::ArrayRef, llvm::SmallVectorImpl ®ions) { static_assert(BranchYieldOp::hasTrait< @@ -477,6 +477,7 @@ namespace py { llvm_unreachable("BranchYieldOp has unexpected parent op kind"); }); + (void)result; assert(result.succeeded()); } diff --git a/src/executable/mlir/Dialect/Python/MLIRGenerator.cpp b/src/executable/mlir/Dialect/Python/MLIRGenerator.cpp index 0b7d4257..b8d7579b 100644 --- a/src/executable/mlir/Dialect/Python/MLIRGenerator.cpp +++ b/src/executable/mlir/Dialect/Python/MLIRGenerator.cpp @@ -38,8 +38,9 @@ namespace { mlir::Location loc(mlir::OpBuilder &builder, std::string_view filename, const SourceLocation &loc) { - return mlir::FileLineColLoc::get( - builder.getStringAttr(filename), loc.start.row, loc.start.column); + return mlir::FileLineColLoc::get(builder.getStringAttr(filename), + static_cast(loc.start.row), + static_cast(loc.start.column)); } void add_name(mlir::OpBuilder &builder, mlir::StringRef name, mlir::Operation *fn) @@ -133,15 +134,15 @@ mlir::py::ConstantOp load_const(mlir::OpBuilder &builder, builder.getIntegerType(1, false), llvm::APInt::getZero(1)); } else { const size_t bits = mpz_sizeinbase(integer.get_mpz_t(), 2); - return builder.getIntegerAttr( - builder.getIntegerType(bits, integer.get_mpz_t()->_mp_size < 0), - llvm::APInt(bits, + return builder.getIntegerAttr(builder.getIntegerType(static_cast(bits), + integer.get_mpz_t()->_mp_size < 0), + llvm::APInt(static_cast(bits), llvm::ArrayRef( integer.get_mpz_t()->_mp_d, std::abs(integer.get_mpz_t()->_mp_size)))); } }(); - auto op = builder.create(loc(builder, filename, source_location), value); + auto op = mlir::py::ConstantOp::create(builder, loc(builder, filename, source_location), value); return op; } @@ -150,8 +151,8 @@ mlir::py::ConstantOp load_const(mlir::OpBuilder &builder, std::string_view filename, SourceLocation source_location) { - return builder.create( - loc(builder, filename, source_location), builder.getStringAttr(str)); + return mlir::py::ConstantOp::create( + builder, loc(builder, filename, source_location), builder.getStringAttr(str)); } /// Find the first parent operation of the given type, or nullptr if there is @@ -268,13 +269,13 @@ template MLIRGenerator::MLIRValue *MLIRGenerator::new_value(Ar } -ast::Value *MLIRGenerator::visit(const ast::Argument *node) +ast::Value *MLIRGenerator::visit(const ast::Argument *) { TODO(); return nullptr; } -ast::Value *MLIRGenerator::visit(const ast::Arguments *node) +ast::Value *MLIRGenerator::visit(const ast::Arguments *) { TODO(); return nullptr; @@ -285,7 +286,7 @@ ast::Value *MLIRGenerator::visit(const ast::Attribute *node) auto self = static_cast(node->value()->codegen(this))->value; switch (node->context()) { case ast::ContextType::LOAD: { - return new_value(m_context.builder().create( + return new_value(mlir::py::LoadAttributeOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), self, @@ -295,7 +296,7 @@ ast::Value *MLIRGenerator::visit(const ast::Attribute *node) TODO(); } break; case ast::ContextType::DELETE: { - m_context.builder().create( + mlir::py::DeleteAttributeOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), self, m_context.builder().getStringAttr(node->attr())); @@ -330,24 +331,32 @@ void MLIRGenerator::store_name(std::string_view name, switch (visibility) { case VariablesResolver::Visibility::NAME: { - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name, value->value); + mlir::py::StoreNameOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), location), + name, + value->value); } break; case VariablesResolver::Visibility::LOCAL: { - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name, value->value); + mlir::py::StoreFastOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), location), + name, + value->value); } break; case VariablesResolver::Visibility::EXPLICIT_GLOBAL: case VariablesResolver::Visibility::IMPLICIT_GLOBAL: { if (&m_scope.front() == &scope()) { - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name, value->value); + mlir::py::StoreNameOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), location), + name, + value->value); } else { auto current_fn = getParentOfType( m_context.builder().getInsertionBlock()->getParent()); add_name(m_context.builder(), name, current_fn); - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name, value->value); + mlir::py::StoreGlobalOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), location), + name, + value->value); } } break; case VariablesResolver::Visibility::CELL: { @@ -357,8 +366,10 @@ void MLIRGenerator::store_name(std::string_view name, ASSERT(std::find_if(arr.begin(), arr.end(), [name](mlir::Attribute attr) { return mlir::cast(attr).getValue() == mlir::StringRef{ name }; }) != arr.end()); - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name, value->value); + mlir::py::StoreDerefOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), location), + name, + value->value); } break; case VariablesResolver::Visibility::FREE: { auto parent = getParentOfType( @@ -367,12 +378,16 @@ void MLIRGenerator::store_name(std::string_view name, ASSERT(std::find_if(arr.begin(), arr.end(), [name](mlir::Attribute attr) { return mlir::cast(attr).getValue() == mlir::StringRef{ name }; }) != arr.end()); - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name, value->value); + mlir::py::StoreDerefOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), location), + name, + value->value); } break; case VariablesResolver::Visibility::HIDDEN: { - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name, value->value); + mlir::py::StoreNameOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), location), + name, + value->value); } break; } } @@ -397,13 +412,13 @@ MLIRGenerator::MLIRValue *MLIRGenerator::load_name(std::string_view name, switch (visibility) { case VariablesResolver::Visibility::NAME: { - return new_value(m_context.builder().create( + return new_value(mlir::py::LoadNameOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), name)); } case VariablesResolver::Visibility::LOCAL: { - return new_value(m_context.builder().create( + return new_value(mlir::py::LoadFastOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), name)); @@ -411,7 +426,7 @@ MLIRGenerator::MLIRValue *MLIRGenerator::load_name(std::string_view name, case VariablesResolver::Visibility::EXPLICIT_GLOBAL: case VariablesResolver::Visibility::IMPLICIT_GLOBAL: { if (&m_scope.front() == &scope()) { - return new_value(m_context.builder().create( + return new_value(mlir::py::LoadNameOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), name)); @@ -419,7 +434,7 @@ MLIRGenerator::MLIRValue *MLIRGenerator::load_name(std::string_view name, auto parent = getParentOfType( m_context.builder().getInsertionBlock()->getParent()); add_name(m_context.builder(), name, parent); - return new_value(m_context.builder().create( + return new_value(mlir::py::LoadGlobalOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), name)); @@ -431,7 +446,7 @@ MLIRGenerator::MLIRValue *MLIRGenerator::load_name(std::string_view name, ASSERT(std::find_if(arr.begin(), arr.end(), [name](mlir::Attribute attr) { return mlir::cast(attr).getValue() == mlir::StringRef{ name }; }) != arr.end()); - return new_value(m_context.builder().create( + return new_value(mlir::py::LoadDerefOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), name)); @@ -443,13 +458,13 @@ MLIRGenerator::MLIRValue *MLIRGenerator::load_name(std::string_view name, ASSERT(std::find_if(arr.begin(), arr.end(), [name](mlir::Attribute attr) { return mlir::cast(attr).getValue() == mlir::StringRef{ name }; }) != arr.end()); - return new_value(m_context.builder().create( + return new_value(mlir::py::LoadDerefOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), name)); } break; case VariablesResolver::Visibility::HIDDEN: { - return new_value(m_context.builder().create( + return new_value(mlir::py::LoadNameOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), name)); @@ -484,35 +499,37 @@ void MLIRGenerator::delete_name(std::string_view name, const SourceLocation &loc m_context.builder().getInsertionBlock()->getParent()); add_name(m_context.builder(), name, current_fn); if (&m_scope.front() == &scope()) { - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name); + mlir::py::DeleteNameOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), location), + name); } else { - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name); + mlir::py::DeleteGlobalOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), location), + name); } } break; case VariablesResolver::Visibility::NAME: { auto current_fn = getParentOfType( m_context.builder().getInsertionBlock()->getParent()); add_name(m_context.builder(), name, current_fn); - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name); + mlir::py::DeleteNameOp::create( + m_context.builder(), loc(m_context.builder(), m_context.filename(), location), name); } break; case VariablesResolver::Visibility::LOCAL: { - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name); + mlir::py::DeleteFastOp::create( + m_context.builder(), loc(m_context.builder(), m_context.filename(), location), name); } break; case VariablesResolver::Visibility::CELL: case VariablesResolver::Visibility::FREE: { - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name); + mlir::py::DeleteDerefOp::create( + m_context.builder(), loc(m_context.builder(), m_context.filename(), location), name); } break; case VariablesResolver::Visibility::HIDDEN: { auto current_fn = getParentOfType( m_context.builder().getInsertionBlock()->getParent()); add_name(m_context.builder(), name, current_fn); - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), location), name); + mlir::py::DeleteNameOp::create( + m_context.builder(), loc(m_context.builder(), m_context.filename(), location), name); } break; } } @@ -526,7 +543,7 @@ void MLIRGenerator::assign(const ast::ASTNode *target, } else if (auto subscript = as(target)) { auto value = static_cast(*subscript->value()->codegen(this)).value; auto index = build_slice(subscript->slice(), subscript->source_location())->value; - m_context.builder().create( + mlir::py::StoreSubscriptOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), source_location), value, index, @@ -542,7 +559,7 @@ void MLIRGenerator::assign(const ast::ASTNode *target, std::vector unpacked_types( tuple->elements().size() - 1, m_context->pyobject_type()); mlir::Type rest{ m_context->pyobject_type() }; - auto unpack_sequence = m_context.builder().create( + auto unpack_sequence = mlir::py::UnpackExpandOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), source_location), unpacked_types, rest, @@ -561,7 +578,7 @@ void MLIRGenerator::assign(const ast::ASTNode *target, std::vector unpacked_values; std::vector unpacked_types( tuple->elements().size(), m_context->pyobject_type()); - auto unpack_sequence = m_context.builder().create( + auto unpack_sequence = mlir::py::UnpackSequenceOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), source_location), unpacked_types, src->value); @@ -573,8 +590,11 @@ void MLIRGenerator::assign(const ast::ASTNode *target, } else if (auto attr = as(target)) { auto obj = static_cast(*attr->value()->codegen(this)).value; const auto &name = attr->attr(); - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), source_location), obj, name, src->value); + mlir::py::StoreAttributeOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), source_location), + obj, + name, + src->value); } else { ASSERT(false && "Invalid assignment in AST"); } @@ -592,7 +612,7 @@ ast::Value *MLIRGenerator::visit(const ast::Assign *node) ast::Value *MLIRGenerator::visit(const ast::Assert *node) { auto test = static_cast(*node->test()->codegen(this)).value; - auto cond = m_context.builder().create( + auto cond = mlir::py::CastToBoolOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->test()->source_location()), m_context.builder().getI1Type(), test); @@ -602,7 +622,7 @@ ast::Value *MLIRGenerator::visit(const ast::Assert *node) auto continuation = m_context.builder().createBlock(parent); m_context.builder().setInsertionPointToEnd(cond.getOperation()->getBlock()); - m_context.builder().create( + mlir::cf::CondBranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->test()->source_location()), cond, continuation, @@ -617,13 +637,13 @@ ast::Value *MLIRGenerator::visit(const ast::Assert *node) const auto assert_location = static_cast((static_cast(assert_start.row) << 32) | (static_cast(assert_start.column) & 0xFFFFFFFFull)); - auto assertion_error_fn = m_context.builder().create( + auto assertion_error_fn = mlir::py::LoadAssertionError::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), m_context.builder().getI64IntegerAttr(assert_location)); if (node->msg()) { auto msg = static_cast(*node->msg()->codegen(this)).value; - return m_context.builder().create( + return mlir::py::FunctionCallOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), assertion_error_fn, @@ -635,7 +655,7 @@ ast::Value *MLIRGenerator::visit(const ast::Assert *node) false, false); } else { - return m_context.builder().create( + return mlir::py::FunctionCallOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), assertion_error_fn, @@ -649,8 +669,9 @@ ast::Value *MLIRGenerator::visit(const ast::Assert *node) } }(); - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), node->source_location()), assertion_error); + mlir::py::RaiseOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), node->source_location()), + assertion_error); m_context.builder().setInsertionPointToEnd(continuation); @@ -675,11 +696,11 @@ ast::Value *MLIRGenerator::visit(const ast::AsyncFunctionDefinition *node) ast::Value *MLIRGenerator::visit(const ast::Await *node) { auto iterable = static_cast(*node->value()->codegen(this)).value; - auto iterator = m_context.builder().create( + auto iterator = mlir::py::GetAwaitableOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->value()->source_location()), m_context->pyobject_type(), iterable); - return new_value(m_context.builder().create( + return new_value(mlir::py::YieldFromOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), iterator)); @@ -697,7 +718,7 @@ ast::Value *MLIRGenerator::visit(const ast::AugAssign *node) } else if (auto attribute_target = as(node->target())) { target_value = static_cast(*attribute_target->value()->codegen(this)).value; - return new_value(m_context.builder().create( + return new_value(mlir::py::LoadAttributeOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), *target_value, @@ -707,7 +728,7 @@ ast::Value *MLIRGenerator::visit(const ast::AugAssign *node) static_cast(*subscript_target->value()->codegen(this)).value; target_slice = build_slice(subscript_target->slice(), subscript_target->source_location())->value; - return new_value(m_context.builder().create( + return new_value(mlir::py::BinarySubscriptOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), *target_value, @@ -720,10 +741,10 @@ ast::Value *MLIRGenerator::visit(const ast::AugAssign *node) }(); auto value = node->value()->codegen(this); - auto result = [&]() { + [[maybe_unused]] auto result = [&]() { auto make_binop = [this, &node]( ast::Value *value, ast::Value *target, mlir::py::ArithOpKind kind) { - return new_value(m_context.builder().create( + return new_value(mlir::py::InplaceOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), static_cast(value)->value, @@ -781,7 +802,7 @@ ast::Value *MLIRGenerator::visit(const ast::AugAssign *node) } else if (auto attribute_target = as(node->target())) { ASSERT(target_value.has_value()); const auto &name = attribute_target->attr(); - m_context.builder().create( + mlir::py::StoreAttributeOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->target()->source_location()), *target_value, name, @@ -789,7 +810,7 @@ ast::Value *MLIRGenerator::visit(const ast::AugAssign *node) } else if (as(node->target())) { ASSERT(target_value.has_value()); ASSERT(target_slice.has_value()); - m_context.builder().create( + mlir::py::StoreSubscriptOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->target()->source_location()), *target_value, *target_slice, @@ -806,10 +827,11 @@ ast::Value *MLIRGenerator::visit(const ast::Break *node) auto *old_b = m_context.builder().getBlock(); auto *b = m_context.builder().createBlock(old_b->getParent()); m_context.builder().setInsertionPointToEnd(old_b); - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), node->source_location()), b); + mlir::cf::BranchOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), node->source_location()), + b); m_context.builder().setInsertionPointToStart(b); - m_context.builder().create( + mlir::py::BranchYieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), mlir::py::LoopOpKindAttr::get(&m_context.ctx(), mlir::py::LoopOpKind::break_)); return nullptr; @@ -822,7 +844,8 @@ ast::Value *MLIRGenerator::visit(const ast::BinaryExpr *node) auto location = loc(m_context.builder(), m_context.filename(), node->source_location()); auto build_binary = [&](mlir::py::ArithOpKind kind) { - return new_value(m_context.builder().create(location, + return new_value(mlir::py::BinaryOp::create(m_context.builder(), + location, m_context->pyobject_type(), mlir::py::ArithOpKindAttr::get(&m_context.ctx(), kind), lhs, @@ -877,19 +900,19 @@ ast::Value *MLIRGenerator::visit(const ast::BoolOp *node) while (std::next(it) != end) { auto *result_block = m_context.builder().createBlock(continuation); m_context.builder().setInsertionPointToEnd(current); - m_context.builder().create( + mlir::cf::BranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), (*it)->source_location()), result_block); m_context.builder().setInsertionPointToStart(result_block); auto result = static_cast((*it)->codegen(this))->value; - auto cond = m_context.builder().create( + auto cond = mlir::py::CastToBoolOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), (*it)->source_location()), m_context.builder().getI1Type(), result); auto *this_block = m_context.builder().getInsertionBlock(); auto *next = m_context.builder().createBlock(continuation); m_context.builder().setInsertionPointToEnd(this_block); - m_context.builder().create( + mlir::cf::CondBranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), (*it)->source_location()), cond, next, @@ -901,7 +924,7 @@ ast::Value *MLIRGenerator::visit(const ast::BoolOp *node) m_context.builder().setInsertionPointToEnd(current); } auto result = static_cast((*it)->codegen(this))->value; - m_context.builder().create( + mlir::cf::BranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), (*it)->source_location()), continuation, mlir::ValueRange{ result }); @@ -913,19 +936,19 @@ ast::Value *MLIRGenerator::visit(const ast::BoolOp *node) while (std::next(it) != end) { auto *result_block = m_context.builder().createBlock(continuation); m_context.builder().setInsertionPointToEnd(current); - m_context.builder().create( + mlir::cf::BranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), (*it)->source_location()), result_block); m_context.builder().setInsertionPointToStart(result_block); auto result = static_cast((*it)->codegen(this))->value; - auto cond = m_context.builder().create( + auto cond = mlir::py::CastToBoolOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), (*it)->source_location()), m_context.builder().getI1Type(), result); auto *this_block = m_context.builder().getInsertionBlock(); auto *next = m_context.builder().createBlock(continuation); m_context.builder().setInsertionPointToEnd(this_block); - m_context.builder().create( + mlir::cf::CondBranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), (*it)->source_location()), cond, continuation, @@ -937,7 +960,7 @@ ast::Value *MLIRGenerator::visit(const ast::BoolOp *node) m_context.builder().setInsertionPointToEnd(current); } auto result = static_cast((*it)->codegen(this))->value; - m_context.builder().create( + mlir::cf::BranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), (*it)->source_location()), continuation, mlir::ValueRange{ result }); @@ -954,7 +977,7 @@ ast::Value *MLIRGenerator::visit(const ast::Call *node) if (auto method = as(node->function())) { auto self = static_cast(method->value()->codegen(this))->value; auto method_name = method->attr(); - return m_context.builder().create( + return mlir::py::LoadMethodOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), self, @@ -991,9 +1014,8 @@ ast::Value *MLIRGenerator::visit(const ast::Call *node) arg_requires_expansion.push_back(is_args_expansion(arg)); args.push_back(arg_value); } - arg_values.push_back(static_cast( - build_tuple(args, arg_requires_expansion, node->source_location())) - ->value); + arg_values.push_back( + build_tuple(args, arg_requires_expansion, node->source_location())->value); } if (!node->keywords().empty()) { requires_kwargs_expansion = true; } { @@ -1004,7 +1026,7 @@ ast::Value *MLIRGenerator::visit(const ast::Call *node) values.reserve(node->keywords().size()); kwarg_requires_expansion.reserve(node->keywords().size()); - auto none = new_value(m_context.builder().create( + auto none = new_value(mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context.builder().getNoneType())); for (const auto &kwarg : node->keywords()) { @@ -1014,14 +1036,13 @@ ast::Value *MLIRGenerator::visit(const ast::Call *node) keys.push_back(none); } else { auto name = *kwarg->arg(); - keys.push_back(new_value(m_context.builder().create( + keys.push_back(new_value(mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), kwarg->source_location()), m_context.builder().getStringAttr(name)))); } } - keyword_values.push_back(static_cast( - build_dict(keys, values, kwarg_requires_expansion, node->source_location())) - ->value); + keyword_values.push_back( + build_dict(keys, values, kwarg_requires_expansion, node->source_location())->value); } } else { arg_values.reserve(node->args().size()); @@ -1041,7 +1062,7 @@ ast::Value *MLIRGenerator::visit(const ast::Call *node) } } - auto function_call = m_context.builder().create( + auto function_call = mlir::py::FunctionCallOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), callee, @@ -1091,7 +1112,7 @@ ast::Value *MLIRGenerator::visit(const ast::ClassDefinition *node) kwargs.push_back(static_cast(keyword->codegen(this))->value); } - auto output = m_context.builder().create( + auto output = mlir::py::ClassDefinitionOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), node->name(), @@ -1150,7 +1171,7 @@ ast::Value *MLIRGenerator::visit(const ast::ClassDefinition *node) for (const auto &el : node->body()) { el->codegen(this); } - m_context.builder().create( + mlir::cf::BranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->body().back()->source_location()), return_block); @@ -1158,13 +1179,14 @@ ast::Value *MLIRGenerator::visit(const ast::ClassDefinition *node) if (class_scope->requires_class_ref) { auto *__class__ = load_name("__class__", node->source_location()); store_name("__classcell__", __class__, node->source_location()); - m_context.builder().create( - m_context.builder().getUnknownLoc(), __class__->value); + mlir::py::ClassReturnOp::create( + m_context.builder(), m_context.builder().getUnknownLoc(), __class__->value); } else { - auto result = m_context.builder().create( - m_context.builder().getUnknownLoc(), m_context.builder().getNoneType()); - m_context.builder().create( - m_context.builder().getUnknownLoc(), result); + auto result = mlir::py::ConstantOp::create(m_context.builder(), + m_context.builder().getUnknownLoc(), + m_context.builder().getNoneType()); + mlir::py::ClassReturnOp::create( + m_context.builder(), m_context.builder().getUnknownLoc(), result); } } @@ -1180,7 +1202,8 @@ ast::Value *MLIRGenerator::visit(const ast::ClassDefinition *node) if (!decorator_functions.empty()) { mlir::Value arg = load_name(node->name(), node->source_location())->value; for (const auto &decorator_function : decorator_functions | std::ranges::views::reverse) { - arg = m_context.builder().create(decorator_function.getLoc(), + arg = mlir::py::FunctionCallOp::create(m_context.builder(), + decorator_function.getLoc(), m_context->pyobject_type(), decorator_function, mlir::ValueRange{ arg }, @@ -1202,10 +1225,11 @@ ast::Value *MLIRGenerator::visit(const ast::Continue *node) auto *old_b = m_context.builder().getBlock(); auto *b = m_context.builder().createBlock(old_b->getParent()); m_context.builder().setInsertionPointToEnd(old_b); - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), node->source_location()), b); + mlir::cf::BranchOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), node->source_location()), + b); m_context.builder().setInsertionPointToStart(b); - m_context.builder().create( + mlir::py::BranchYieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), mlir::py::LoopOpKindAttr::get(&m_context.ctx(), mlir::py::LoopOpKind::continue_)); return nullptr; @@ -1224,7 +1248,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) switch (op) { case ast::Compare::OpType::Eq: { - result = m_context.builder().create( + result = mlir::py::CompareOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), mlir::py::CmpPredicateAttr::get(&m_context.ctx(), mlir::py::CmpPredicate::eq), @@ -1232,7 +1256,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) rhs); } break; case ast::Compare::OpType::NotEq: { - result = m_context.builder().create( + result = mlir::py::CompareOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), mlir::py::CmpPredicateAttr::get(&m_context.ctx(), mlir::py::CmpPredicate::ne), @@ -1240,7 +1264,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) rhs); } break; case ast::Compare::OpType::Lt: { - result = m_context.builder().create( + result = mlir::py::CompareOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), mlir::py::CmpPredicateAttr::get(&m_context.ctx(), mlir::py::CmpPredicate::lt), @@ -1248,7 +1272,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) rhs); } break; case ast::Compare::OpType::LtE: { - result = m_context.builder().create( + result = mlir::py::CompareOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), mlir::py::CmpPredicateAttr::get(&m_context.ctx(), mlir::py::CmpPredicate::le), @@ -1256,7 +1280,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) rhs); } break; case ast::Compare::OpType::Gt: { - result = m_context.builder().create( + result = mlir::py::CompareOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), mlir::py::CmpPredicateAttr::get(&m_context.ctx(), mlir::py::CmpPredicate::gt), @@ -1264,7 +1288,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) rhs); } break; case ast::Compare::OpType::GtE: { - result = m_context.builder().create( + result = mlir::py::CompareOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), mlir::py::CmpPredicateAttr::get(&m_context.ctx(), mlir::py::CmpPredicate::ge), @@ -1272,7 +1296,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) rhs); } break; case ast::Compare::OpType::Is: { - result = m_context.builder().create( + result = mlir::py::CompareOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), mlir::py::CmpPredicateAttr::get(&m_context.ctx(), mlir::py::CmpPredicate::is), @@ -1280,7 +1304,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) rhs); } break; case ast::Compare::OpType::IsNot: { - result = m_context.builder().create( + result = mlir::py::CompareOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), mlir::py::CmpPredicateAttr::get(&m_context.ctx(), mlir::py::CmpPredicate::isnot), @@ -1288,7 +1312,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) rhs); } break; case ast::Compare::OpType::In: { - result = m_context.builder().create( + result = mlir::py::CompareOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), mlir::py::CmpPredicateAttr::get(&m_context.ctx(), mlir::py::CmpPredicate::in), @@ -1296,7 +1320,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) rhs); } break; case ast::Compare::OpType::NotIn: { - result = m_context.builder().create( + result = mlir::py::CompareOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), mlir::py::CmpPredicateAttr::get(&m_context.ctx(), mlir::py::CmpPredicate::notin), @@ -1311,7 +1335,7 @@ ast::Value *MLIRGenerator::visit(const ast::Compare *node) return new_value(*result); } -ast::Value *MLIRGenerator::visit(const ast::Comprehension *node) +ast::Value *MLIRGenerator::visit(const ast::Comprehension *) { ASSERT_NOT_REACHED(); return nullptr; @@ -1326,7 +1350,7 @@ ast::Value *MLIRGenerator::visit(const ast::Constant *node) return std::visit(overloaded{ [this, node](double value) { mlir::py::ConstantOp op = - m_context.builder().create( + mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), @@ -1346,7 +1370,7 @@ ast::Value *MLIRGenerator::visit(const ast::Constant *node) return std::visit( overloaded{ [this, node](bool value) { - auto op = m_context.builder().create( + auto op = mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), @@ -1354,7 +1378,7 @@ ast::Value *MLIRGenerator::visit(const ast::Constant *node) return new_value(op); }, [this, node](py::NoneType) { - auto op = m_context.builder().create( + auto op = mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), @@ -1369,12 +1393,13 @@ ast::Value *MLIRGenerator::visit(const ast::Constant *node) m_context.builder(), s.s, m_context.filename(), node->source_location())); }, [this, node](const py::Bytes &b) -> ast::Value * { - mlir::py::ConstantOp op = m_context.builder().create( - loc(m_context.builder(), m_context.filename(), node->source_location()), b.b); + mlir::py::ConstantOp op = mlir::py::ConstantOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), node->source_location()), + b.b); return new_value(op); }, [this, node](py::Ellipsis) -> ast::Value * { - mlir::py::ConstantOp op = m_context.builder().create( + mlir::py::ConstantOp op = mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), mlir::py::EllipsisAttr::get(&m_context.ctx())); return new_value(op); @@ -1399,7 +1424,7 @@ ast::Value *MLIRGenerator::visit(const ast::Dict *node) std::vector values; std::vector requires_expansion; - auto none = new_value(m_context.builder().create( + auto none = new_value(mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context.builder().getNoneType())); @@ -1425,7 +1450,7 @@ ast::Value *MLIRGenerator::visit(const ast::DictComp *node) [this, node](MLIRValue *container) { auto key = static_cast(node->key()->codegen(this))->value; auto value = static_cast(node->value()->codegen(this))->value; - m_context.builder().create( + mlir::py::DictAddOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->key()->source_location()), container->value, key, @@ -1435,13 +1460,13 @@ ast::Value *MLIRGenerator::visit(const ast::DictComp *node) node->source_location()); } -ast::Value *MLIRGenerator::visit(const ast::ExceptHandler *node) +ast::Value *MLIRGenerator::visit(const ast::ExceptHandler *) { TODO(); return nullptr; } -ast::Value *MLIRGenerator::visit(const ast::Expression *node) +ast::Value *MLIRGenerator::visit(const ast::Expression *) { TODO(); return nullptr; @@ -1449,11 +1474,10 @@ ast::Value *MLIRGenerator::visit(const ast::Expression *node) ast::Value *MLIRGenerator::visit(const ast::For *node) { - auto *parent = m_context.builder().getBlock()->getParent(); - auto iterable = static_cast(node->iter()->codegen(this))->value; - auto for_loop = m_context.builder().create( - loc(m_context.builder(), m_context.filename(), node->source_location()), iterable); + auto for_loop = mlir::py::ForLoopOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), node->source_location()), + iterable); auto &body_start = for_loop.getBody().emplaceBlock(); auto &orelse = for_loop.getOrelse(); @@ -1465,7 +1489,7 @@ ast::Value *MLIRGenerator::visit(const ast::For *node) m_context->pyobject_type(), m_context.builder().getUnknownLoc())); assign(node->target(), iterator, node->target()->source_location()); - m_context.builder().create(m_context.builder().getUnknownLoc()); + mlir::py::BranchYieldOp::create(m_context.builder(), m_context.builder().getUnknownLoc()); m_context.builder().setInsertionPointToStart(&body_start); for (const auto &el : node->body()) { el->codegen(this); } @@ -1474,7 +1498,7 @@ ast::Value *MLIRGenerator::visit(const ast::For *node) .getInsertionBlock() ->back() .hasTrait()) { - m_context.builder().create(m_context.builder().getUnknownLoc()); + mlir::py::BranchYieldOp::create(m_context.builder(), m_context.builder().getUnknownLoc()); } if (!node->orelse().empty()) { @@ -1482,8 +1506,10 @@ ast::Value *MLIRGenerator::visit(const ast::For *node) for (const auto &el : node->orelse()) { el->codegen(this); } if (m_context.builder().getBlock()->empty() || !m_context.builder().getBlock()->back().hasTrait()) { - m_context.builder().create(loc( - m_context.builder(), m_context.filename(), node->body().back()->source_location())); + mlir::py::BranchYieldOp::create(m_context.builder(), + loc(m_context.builder(), + m_context.filename(), + node->body().back()->source_location())); } } @@ -1497,7 +1523,7 @@ ast::Value *MLIRGenerator::visit(const ast::FormattedValue *node) if (node->format_spec()) { TODO(); } auto *value = static_cast(node->value()->codegen(this)); ASSERT(value); - return new_value(m_context.builder().create( + return new_value(mlir::py::FormatValueOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), value->value, @@ -1527,7 +1553,7 @@ ast::Value *MLIRGenerator::visit(const ast::GeneratorExp *node) [this, node]() { return nullptr; }, [this, node](MLIRValue *) { auto result = static_cast(node->elt()->codegen(this))->value; - m_context.builder().create( + mlir::py::YieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->elt()->source_location()), m_context->pyobject_type(), result); @@ -1545,7 +1571,7 @@ ast::Value *MLIRGenerator::visit(const ast::Global *) ast::Value *MLIRGenerator::visit(const ast::If *node) { auto test = static_cast(node->test()->codegen(this))->value; - auto cond = m_context.builder().create( + auto cond = mlir::py::CastToBoolOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->test()->source_location()), m_context.builder().getI1Type(), test); @@ -1556,7 +1582,7 @@ ast::Value *MLIRGenerator::visit(const ast::If *node) auto continuation = m_context.builder().createBlock(parent); m_context.builder().setInsertionPointToEnd(cond.getOperation()->getBlock()); - m_context.builder().create( + mlir::cf::CondBranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->test()->source_location()), cond, if_block, @@ -1566,7 +1592,7 @@ ast::Value *MLIRGenerator::visit(const ast::If *node) for (const auto &el : node->body()) { el->codegen(this); } if (m_context.builder().getBlock()->empty() || !m_context.builder().getBlock()->back().hasTrait()) { - m_context.builder().create( + mlir::cf::BranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->body().back()->source_location()), continuation); } @@ -1575,7 +1601,7 @@ ast::Value *MLIRGenerator::visit(const ast::If *node) for (const auto &el : node->orelse()) { el->codegen(this); } if (m_context.builder().getBlock()->empty() || !m_context.builder().getBlock()->back().hasTrait()) { - m_context.builder().create( + mlir::cf::BranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->orelse().back()->source_location()), @@ -1594,7 +1620,7 @@ ast::Value *MLIRGenerator::visit(const ast::If *node) ast::Value *MLIRGenerator::visit(const ast::IfExpr *node) { auto test = static_cast(node->test()->codegen(this))->value; - auto cond = m_context.builder().create( + auto cond = mlir::py::CastToBoolOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->test()->source_location()), m_context.builder().getI1Type(), test); @@ -1607,7 +1633,7 @@ ast::Value *MLIRGenerator::visit(const ast::IfExpr *node) loc(m_context.builder(), m_context.filename(), node->source_location())); m_context.builder().setInsertionPointToEnd(cond.getOperation()->getBlock()); - m_context.builder().create( + mlir::cf::CondBranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->test()->source_location()), cond, if_block, @@ -1615,14 +1641,14 @@ ast::Value *MLIRGenerator::visit(const ast::IfExpr *node) m_context.builder().setInsertionPointToStart(if_block); auto true_case = static_cast(node->body()->codegen(this))->value; - m_context.builder().create( + mlir::cf::BranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->body()->source_location()), continuation, mlir::ValueRange{ true_case }); m_context.builder().setInsertionPointToStart(orelse_block); auto false_case = static_cast(node->orelse()->codegen(this))->value; - m_context.builder().create( + mlir::cf::BranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->orelse()->source_location()), continuation, mlir::ValueRange{ false_case }); @@ -1639,7 +1665,7 @@ ast::Value *MLIRGenerator::visit(const ast::Import *node) auto from_list = m_context.builder().getStrArrayAttr({}); const uint32_t level = 0; - auto module = new_value(m_context.builder().create( + auto module = new_value(mlir::py::ImportOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), n.name, @@ -1668,19 +1694,20 @@ ast::Value *MLIRGenerator::visit(const ast::ImportFrom *node) auto from_list = m_context.builder().getStrArrayAttr(names); - auto module = m_context.builder().create( + auto module = mlir::py::ImportOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), node->module(), from_list, - node->level()); + static_cast(node->level())); for (const auto &n : node->names()) { if (n.name == "*") { - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), node->source_location()), module); + mlir::py::ImportAllOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), node->source_location()), + module); } else { - auto imported_object = m_context.builder().create( + auto imported_object = mlir::py::ImportFromOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), module, @@ -1722,7 +1749,7 @@ ast::Value *MLIRGenerator::visit(const ast::JoinedStr *node) strings.push_back(load_const( m_context.builder(), current_string.s, m_context.filename(), node->source_location())); } - return new_value(m_context.builder().create( + return new_value(mlir::py::BuildStringOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), strings)); @@ -1770,7 +1797,7 @@ ast::Value *MLIRGenerator::visit(const ast::ListComp *node) [this, node]() { return build_list({}, node->source_location()); }, [this, node](MLIRValue *container) { auto result = static_cast(node->elt()->codegen(this))->value; - m_context.builder().create( + mlir::py::ListAppendOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->elt()->source_location()), container->value, result); @@ -1785,10 +1812,10 @@ ast::Value *MLIRGenerator::visit(const ast::Module *m) const auto filename = fs::path(m->filename()).stem(); [[maybe_unused]] auto module_scope = create_nested_scope(filename, filename); m_context.builder().setInsertionPointToEnd(m_context.module().getBody()); - auto module_fn = - m_context.builder().create(m_context.builder().getUnknownLoc(), - "__hidden_init__", - m_context.builder().getFunctionType({}, { m_context->pyobject_type() })); + auto module_fn = mlir::func::FuncOp::create(m_context.builder(), + m_context.builder().getUnknownLoc(), + "__hidden_init__", + m_context.builder().getFunctionType({}, { m_context->pyobject_type() })); // Public visibility: the module entry's side effects (storing names // into the module dict, etc.) escape this MLIR module because the // bytecode runtime invokes it and importers observe the resulting @@ -1811,15 +1838,16 @@ ast::Value *MLIRGenerator::visit(const ast::Module *m) // If a program does not end with a terminator instruction, jump to the exit_block if (m_context.builder().getBlock()->empty() || !m_context.builder().getBlock()->back().hasTrait()) { - m_context.builder().create( - m_context.builder().getUnknownLoc(), exit_block); + mlir::cf::BranchOp::create( + m_context.builder(), m_context.builder().getUnknownLoc(), exit_block); } m_context.builder().setInsertionPointToEnd(exit_block); - auto result = m_context.builder().create( - m_context.builder().getUnknownLoc(), m_context.builder().getNoneType()); - m_context.builder().create( - m_context.builder().getUnknownLoc(), mlir::ValueRange{ result }); + auto result = mlir::py::ConstantOp::create(m_context.builder(), + m_context.builder().getUnknownLoc(), + m_context.builder().getNoneType()); + mlir::func::ReturnOp::create( + m_context.builder(), m_context.builder().getUnknownLoc(), mlir::ValueRange{ result }); return nullptr; } @@ -1844,7 +1872,7 @@ ast::Value *MLIRGenerator::visit(const ast::Name *node) ASSERT(node->ids().size() == 1); const auto name = node->ids()[0]; if (node->context_type() == ast::ContextType::LOAD) { - // return new_value(m_context.builder().create( + // return new_value(mlir::py::LoadNameOp::create(m_context.builder(), // loc(m_context.builder(), m_context.filename(), node->source_location()), // m_context->pyobject_type(),// TODO: propagate type information // name)); @@ -1866,16 +1894,17 @@ ast::Value *MLIRGenerator::visit(const ast::Raise *node) if (node->cause()) { auto exception = static_cast(*node->exception()->codegen(this)).value; auto cause = static_cast(*node->cause()->codegen(this)).value; - m_context.builder().create( + mlir::py::RaiseOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), exception, cause); } else if (node->exception()) { auto exception = static_cast(*node->exception()->codegen(this)).value; - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), node->source_location()), exception); + mlir::py::RaiseOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), node->source_location()), + exception); } else { - m_context.builder().create( + mlir::py::RaiseOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location())); } @@ -1889,8 +1918,9 @@ ast::Value *MLIRGenerator::visit(const ast::Return *node) if (node->value()) { return static_cast(*node->value()->codegen(this)).value; } - return m_context.builder().create( - m_context.builder().getUnknownLoc(), m_context.builder().getNoneType()); + return mlir::py::ConstantOp::create(m_context.builder(), + m_context.builder().getUnknownLoc(), + m_context.builder().getNoneType()); }(); return_value(new_value(value), node->source_location()); @@ -1931,7 +1961,7 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_comprehension( arg_attrs.push_back( m_context.builder().getNamedAttr("llvm.name", m_context.builder().getStringAttr(".0"))); args_attrs.push_back(m_context.builder().getDictionaryAttr(arg_attrs)); - auto f = m_context.builder().create( + auto f = mlir::func::FuncOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), source_location), mangled_name, func_type, @@ -1966,10 +1996,10 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_comprehension( } auto next_generator = [this](mlir::Value iterable, const ast::Comprehension *generator) { - auto for_loop = m_context.builder().create( + auto for_loop = mlir::py::ForLoopOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), generator->source_location()), iterable); - m_context.builder().create( + mlir::py::BranchYieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), generator->source_location())); // iterator { @@ -1977,8 +2007,8 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_comprehension( auto iterator = new_value(for_loop.getStep().addArgument( m_context->pyobject_type(), m_context.builder().getUnknownLoc())); assign(generator->target(), iterator, generator->target()->source_location()); - m_context.builder().create( - m_context.builder().getUnknownLoc()); + mlir::py::BranchYieldOp::create( + m_context.builder(), m_context.builder().getUnknownLoc()); } // loop body { @@ -1998,12 +2028,12 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_comprehension( next = m_context.builder().createBlock(&body_continue); m_context.builder().setInsertionPointToStart(current); - auto cond = m_context.builder().create( + auto cond = mlir::py::CastToBoolOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), el->source_location()), m_context.builder().getI1Type(), static_cast(el->codegen(this))->value); - m_context.builder().create( + mlir::cf::CondBranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), el->source_location()), cond, next, @@ -2013,18 +2043,18 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_comprehension( } { const auto &el = generator->ifs().back(); - auto cond = m_context.builder().create( + auto cond = mlir::py::CastToBoolOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), el->source_location()), m_context.builder().getI1Type(), static_cast(el->codegen(this))->value); - m_context.builder().create( + mlir::cf::CondBranchOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), el->source_location()), cond, &body_end, &body_continue); } m_context.builder().setInsertionPointToStart(&body_continue); - m_context.builder().create( + mlir::py::BranchYieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), generator->source_location()), @@ -2039,7 +2069,7 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_comprehension( m_context.builder().setInsertionPointToStart(entry_block); auto *container = container_factory(); - auto iterable = m_context.builder().create( + auto iterable = mlir::py::LoadFastOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), first_generator->iter()->source_location()), @@ -2052,7 +2082,7 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_comprehension( next_generator(iter, generator); } container_update(container); - m_context.builder().create(m_context.builder().getUnknownLoc()); + mlir::py::BranchYieldOp::create(m_context.builder(), m_context.builder().getUnknownLoc()); m_context.builder().setInsertionPointToEnd(entry_block); m_context.builder().getBlock()->back().erase(); @@ -2060,7 +2090,7 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_comprehension( return_value(container, first_generator->target()->source_location()); } else { f->setAttr("is_generator", m_context.builder().getBoolAttr(true)); - auto none = m_context.builder().create( + auto none = mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), first_generator->target()->source_location()), @@ -2074,7 +2104,7 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_comprehension( std::vector captures_ref; captures_ref.reserve(captures.size()); for (const auto &el : captures) { captures_ref.push_back(el); } - auto fn_obj = m_context.builder().create( + auto fn_obj = mlir::py::MakeFunctionOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), source_location), m_context->pyobject_type(), mangled_name, @@ -2084,7 +2114,7 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_comprehension( auto iterable = static_cast(generators.front()->iter()->codegen(this))->value; - return new_value(m_context.builder().create( + return new_value(mlir::py::FunctionCallOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), source_location), m_context->pyobject_type(), fn_obj, @@ -2103,7 +2133,7 @@ ast::Value *MLIRGenerator::visit(const ast::SetComp *node) [this, node]() { return build_set({}, node->source_location()); }, [this, node](MLIRValue *container) { auto result = static_cast(node->elt()->codegen(this))->value; - m_context.builder().create( + mlir::py::SetAddOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->elt()->source_location()), container->value, result); @@ -2135,17 +2165,17 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_slice( // pass nullptr when missing and save a register. auto lower = slice.lower ? static_cast(*slice.lower->codegen(this)).value - : m_context.builder().create( + : mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context.builder().getNoneType()); auto upper = slice.upper ? static_cast(*slice.upper->codegen(this)).value - : m_context.builder().create( + : mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context.builder().getNoneType()); auto step = slice.step ? static_cast(*slice.step->codegen(this)).value : mlir::Value{}; - return new_value(m_context.builder().create( + return new_value(mlir::py::BuildSliceOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), lower, @@ -2194,7 +2224,7 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_list( std::back_inserter(requires_expansion_), [](bool el) -> int8_t { return el; }); - return new_value(m_context.builder().create( + return new_value(mlir::py::BuildListOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), elements, @@ -2235,7 +2265,7 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_dict( std::back_inserter(requires_expansion_), [](bool el) -> int8_t { return el; }); - return new_value(m_context.builder().create( + return new_value(mlir::py::BuildDictOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), ks, @@ -2269,7 +2299,7 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_tuple( std::back_inserter(requires_expansion_), [](bool el) -> int8_t { return el; }); - return new_value(m_context.builder().create( + return new_value(mlir::py::BuildTupleOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), elements, @@ -2301,7 +2331,7 @@ codegen::MLIRGenerator::MLIRValue *MLIRGenerator::build_set(std::vector int8_t { return el; }); - return new_value(m_context.builder().create( + return new_value(mlir::py::BuildSetOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), location), m_context->pyobject_type(), std::move(elements), @@ -2312,13 +2342,13 @@ void MLIRGenerator::return_value(MLIRValue *value, const SourceLocation &source_ { for (auto clear_exception : scope().clear_exception_before_return) { if (clear_exception) { - m_context.builder().create( + mlir::py::ClearExceptionStateOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), source_location)); } } if (!scope().finally_blocks.empty()) { - // m_context.builder().create( + // mlir::cf::BranchOp::create(m_context.builder(), // loc(m_context.builder(), m_context.filename(), node->source_location()), // scope().finally_blocks.top()); const auto finally_blocks = scope().finally_blocks; @@ -2331,7 +2361,7 @@ void MLIRGenerator::return_value(MLIRValue *value, const SourceLocation &source_ scope().finally_blocks = std::move(finally_blocks); } - m_context.builder().create( + mlir::py::ReturnOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), source_location), mlir::ValueRange{ value->value }); } @@ -2345,7 +2375,6 @@ MLIRGenerator::RAIIScope MLIRGenerator::setup_function(mlir::func::FuncOp &f, const auto &name_visibility_it = m_variable_visibility.find(mangled_name); ASSERT(name_visibility_it != m_variable_visibility.end()); const auto &symbol_map = name_visibility_it->second->symbol_map; - const bool is_generator = name_visibility_it->second->is_generator; for (const auto &symbol : symbol_map.symbols) { const auto &varname = symbol.name; @@ -2427,7 +2456,8 @@ void apply_decorators_for_make_function(MLIRGenerator &gen, mlir::Value arg = gen.load_name(function_name, source_location)->value; for (auto *decorator : decorator_functions | std::ranges::views::reverse) { mlir::Value decorator_function = decorator->value; - arg = builder.create(decorator_function.getLoc(), + arg = mlir::py::FunctionCallOp::create(builder, + decorator_function.getLoc(), gen.m_context->pyobject_type(), decorator_function, mlir::ValueRange{ arg }, @@ -2507,7 +2537,8 @@ MLIRGenerator::MLIRValue *MLIRGenerator::make_function(const std::string &functi } builder.setInsertionPointToEnd(&m_context.module().getBodyRegion().getBlocks().back()); - auto f = builder.create(loc(builder, m_context.filename(), source_location), + auto f = mlir::func::FuncOp::create(builder, + loc(builder, m_context.filename(), source_location), mangled_name, func_type, mlir::ArrayRef{}, @@ -2526,8 +2557,8 @@ MLIRGenerator::MLIRValue *MLIRGenerator::make_function(const std::string &functi if (builder.getBlock()->empty() || !builder.getBlock()->back().hasTrait()) { - auto none = builder.create( - builder.getUnknownLoc(), builder.getNoneType()); + auto none = mlir::py::ConstantOp::create( + builder, builder.getUnknownLoc(), builder.getNoneType()); return_value(new_value(none), source_location); } } @@ -2542,7 +2573,7 @@ MLIRGenerator::MLIRValue *MLIRGenerator::make_function(const std::string &functi std::vector kw_defaults_values; kw_defaults_values.reserve(kw_defaults.size()); for (auto *v : kw_defaults) { kw_defaults_values.push_back(v->value); } - auto fn_obj = new_value(builder.create( + auto fn_obj = new_value(mlir::py::MakeFunctionOp::create(builder, loc(builder, m_context.filename(), source_location), m_context->pyobject_type(), mangled_name, @@ -2565,12 +2596,14 @@ ast::Value *MLIRGenerator::visit(const ast::Subscript *node) switch (node->context()) { case ast::ContextType::DELETE: { - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), node->source_location()), value, index); + mlir::py::DeleteSubscriptOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), node->source_location()), + value, + index); return nullptr; } break; case ast::ContextType::LOAD: { - return new_value(m_context.builder().create( + return new_value(mlir::py::BinarySubscriptOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), value, @@ -2588,12 +2621,9 @@ ast::Value *MLIRGenerator::visit(const ast::Subscript *node) ast::Value *MLIRGenerator::visit(const ast::Try *node) { - auto *current = m_context.builder().getBlock(); - auto *parent = current->getParent(); - - auto try_op = m_context.builder().create( + auto try_op = mlir::py::TryOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), - node->handlers().size()); + static_cast(node->handlers().size())); if (!try_op.getHandlers().empty()) { scope().unhappy_path.push(&try_op.getHandlers().front().front()); @@ -2604,14 +2634,15 @@ ast::Value *MLIRGenerator::visit(const ast::Try *node) scope().finally_blocks.emplace_back([this, node](bool first) { (void)first; - // if (!first) { m_context.builder().create(); } + // if (!first) mler().()::create m_context.builder(),} // auto current_fn = getParentOfType( // m_context.builder().getInsertionBlock()->getParent()); auto *current = m_context.builder().getInsertionBlock(); auto *final_block = m_context.builder().createBlock(current->getParent()); m_context.builder().setInsertionPointToEnd(current); - m_context.builder().create( - loc(m_context.builder(), m_context.filename(), node->source_location()), final_block); + mlir::cf::BranchOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), node->source_location()), + final_block); m_context.builder().setInsertionPointToEnd(final_block); if (!node->finalbody().empty()) { for (auto el : node->finalbody()) { el->codegen(this); } @@ -2626,7 +2657,7 @@ ast::Value *MLIRGenerator::visit(const ast::Try *node) for (const auto &el : node->body()) { el->codegen(this); } if (m_context.builder().getBlock()->empty() || !m_context.builder().getBlock()->back().hasTrait()) { - m_context.builder().create( + mlir::py::BranchYieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location())); } @@ -2638,19 +2669,18 @@ ast::Value *MLIRGenerator::visit(const ast::Try *node) }); for (auto p : llvm::enumerate(llvm::zip(try_op.getHandlers(), node->handlers()))) { - const auto &idx = p.index(); auto [handler_region, handler] = p.value(); ASSERT(!handler_region.getBlocks().empty()); m_context.builder().setInsertionPointToStart(&handler_region.front()); - auto handler_op = m_context.builder().create( + auto handler_op = mlir::py::TryHandlerOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), handler->source_location())); if (handler->type()) { - auto *exception_check_block = m_context.builder().createBlock(&handler_op.getCond()); + m_context.builder().createBlock(&handler_op.getCond()); auto exception_type = handler->type()->codegen(this); - m_context.builder().create( + mlir::py::ConditionOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), handler->source_location()), static_cast(exception_type)->value); } @@ -2661,7 +2691,7 @@ ast::Value *MLIRGenerator::visit(const ast::Try *node) // *instance* (not the type). Bind it at the start of the matched // handler body via py.load_exception. if (!handler->name().empty()) { - auto exception_instance = m_context.builder().create( + auto exception_instance = mlir::py::LoadException::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), handler->source_location()), m_context->pyobject_type()); store_name(handler->name(), @@ -2675,7 +2705,7 @@ ast::Value *MLIRGenerator::visit(const ast::Try *node) .getBlock() ->back() .hasTrait()) { - m_context.builder().create( + mlir::py::BranchYieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location())); } } @@ -2686,7 +2716,7 @@ ast::Value *MLIRGenerator::visit(const ast::Try *node) for (auto el : node->orelse()) { el->codegen(this); } if (m_context.builder().getBlock()->empty() || !m_context.builder().getBlock()->back().hasTrait()) { - m_context.builder().create( + mlir::py::BranchYieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location())); } } @@ -2699,7 +2729,7 @@ ast::Value *MLIRGenerator::visit(const ast::Try *node) for (auto el : node->finalbody()) { el->codegen(this); } if (m_context.builder().getBlock()->empty() || !m_context.builder().getBlock()->back().hasTrait()) { - m_context.builder().create( + mlir::py::BranchYieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location())); } } @@ -2741,25 +2771,25 @@ ast::Value *MLIRGenerator::visit(const ast::UnaryExpr *node) auto src = static_cast(*node->operand()->codegen(this)).value; switch (node->op_type()) { case ast::UnaryOpType::ADD: { - return new_value(m_context.builder().create( + return new_value(mlir::py::PositiveOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), src)); } break; case ast::UnaryOpType::SUB: { - return new_value(m_context.builder().create( + return new_value(mlir::py::NegativeOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), src)); } break; case ast::UnaryOpType::INVERT: { - return new_value(m_context.builder().create( + return new_value(mlir::py::InvertOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), src)); } break; case ast::UnaryOpType::NOT: { - return new_value(m_context.builder().create( + return new_value(mlir::py::NotOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), src)); @@ -2771,9 +2801,8 @@ ast::Value *MLIRGenerator::visit(const ast::UnaryExpr *node) ast::Value *MLIRGenerator::visit(const ast::While *node) { auto *current_block = m_context.builder().getInsertionBlock(); - auto *parent = current_block->getParent(); - auto while_op = m_context.builder().create( + auto while_op = mlir::py::WhileOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location())); auto &condition = while_op.getCondition(); @@ -2787,14 +2816,14 @@ ast::Value *MLIRGenerator::visit(const ast::While *node) m_context.builder().setInsertionPointToStart(condition_block); auto test = static_cast(node->test()->codegen(this))->value; - m_context.builder().create(test.getLoc(), test); + mlir::py::ConditionOp::create(m_context.builder(), test.getLoc(), test); m_context.builder().setInsertionPointToStart(body_start_block); for (const auto &el : node->body()) { el->codegen(this); } if (m_context.builder().getBlock()->empty() || !m_context.builder().getBlock()->back().hasTrait()) { - m_context.builder().create( + mlir::py::BranchYieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->body().back()->source_location())); } @@ -2803,8 +2832,10 @@ ast::Value *MLIRGenerator::visit(const ast::While *node) for (const auto &el : node->orelse()) { el->codegen(this); } if (m_context.builder().getBlock()->empty() || !m_context.builder().getBlock()->back().hasTrait()) { - m_context.builder().create(loc( - m_context.builder(), m_context.filename(), node->body().back()->source_location())); + mlir::py::BranchYieldOp::create(m_context.builder(), + loc(m_context.builder(), + m_context.filename(), + node->body().back()->source_location())); } } @@ -2823,27 +2854,27 @@ ast::Value *MLIRGenerator::visit(const ast::With *node) } auto *current_block = m_context.builder().getBlock(); - auto *parent = current_block->getParent(); - auto with = m_context.builder().create( - loc(m_context.builder(), m_context.filename(), node->source_location()), with_item_results); + auto with = mlir::py::WithOp::create(m_context.builder(), + loc(m_context.builder(), m_context.filename(), node->source_location()), + with_item_results); auto with_exit_factory = [this, node, &with_item_results](bool first) { ASSERT(node->items().size() == 1); - for (size_t i = 0; const auto &item : with_item_results) { + for (const auto &item : with_item_results) { if (!first) { - // m_context.builder().create(item.getLoc()); + // mlir::py::LeaveExceptionHandling::create(m_context.builder(),item.getLoc()); } - auto exit = m_context.builder().create( - item.getLoc(), m_context->pyobject_type(), item, "__exit__"); + auto exit = mlir::py::LoadMethodOp::create( + m_context.builder(), item.getLoc(), m_context->pyobject_type(), item, "__exit__"); - auto none = m_context.builder().create( + auto none = mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context.builder().getNoneType()); - m_context.builder().create( + mlir::py::FunctionCallOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), exit, @@ -2856,7 +2887,7 @@ ast::Value *MLIRGenerator::visit(const ast::With *node) false); } - m_context.builder().create( + mlir::py::ClearExceptionStateOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location())); }; scope().finally_blocks.push_back(with_exit_factory); @@ -2867,7 +2898,7 @@ ast::Value *MLIRGenerator::visit(const ast::With *node) for (const auto &el : node->body()) { el->codegen(this); } if (m_context.builder().getBlock()->empty() || !m_context.builder().getBlock()->back().hasTrait()) { - m_context.builder().create( + mlir::py::BranchYieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location())); } scope().finally_blocks.pop_back(); @@ -2880,12 +2911,12 @@ ast::Value *MLIRGenerator::visit(const ast::With *node) ast::Value *MLIRGenerator::visit(const ast::WithItem *node) { auto expr = static_cast(node->context_expr()->codegen(this))->value; - auto method = m_context.builder().create( + auto method = mlir::py::LoadMethodOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), expr, "__enter__"); - auto item_result = m_context.builder().create( + auto item_result = mlir::py::FunctionCallOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), method, @@ -2909,12 +2940,12 @@ ast::Value *MLIRGenerator::visit(const ast::Yield *node) if (node->value()) { return static_cast(node->value()->codegen(this))->value; } else { - return m_context.builder().create( + return mlir::py::ConstantOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context.builder().getNoneType()); } }(); - return new_value(m_context.builder().create( + return new_value(mlir::py::YieldOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), value)); @@ -2923,7 +2954,7 @@ ast::Value *MLIRGenerator::visit(const ast::Yield *node) ast::Value *MLIRGenerator::visit(const ast::YieldFrom *node) { auto value = static_cast(node->value()->codegen(this))->value; - return new_value(m_context.builder().create( + return new_value(mlir::py::YieldFromOp::create(m_context.builder(), loc(m_context.builder(), m_context.filename(), node->source_location()), m_context->pyobject_type(), value)); diff --git a/src/executable/mlir/Target/PythonBytecode/CMakeLists.txt b/src/executable/mlir/Target/PythonBytecode/CMakeLists.txt index 9f0140ad..4ef34f3d 100644 --- a/src/executable/mlir/Target/PythonBytecode/CMakeLists.txt +++ b/src/executable/mlir/Target/PythonBytecode/CMakeLists.txt @@ -29,6 +29,8 @@ target_include_directories(TargetPythonBytecode ${CMAKE_BINARY_DIR}/src/executable/mlir/Dialect ) +python_cpp_link_project_options(TargetPythonBytecode) + # ${CMAKE_SOURCE_DIR}/src # ${CMAKE_SOURCE_DIR}/src/executable/mlir/include/mlir/Dialect # ${CMAKE_BINARY_DIR}/src/executable/mlir/include/mlir/Dialect diff --git a/src/executable/mlir/Target/PythonBytecode/LinearScanRegisterAllocation.hpp b/src/executable/mlir/Target/PythonBytecode/LinearScanRegisterAllocation.hpp index a3f3cc01..14ce3280 100644 --- a/src/executable/mlir/Target/PythonBytecode/LinearScanRegisterAllocation.hpp +++ b/src/executable/mlir/Target/PythonBytecode/LinearScanRegisterAllocation.hpp @@ -220,15 +220,15 @@ class LinearScanRegisterAllocation auto *def_op = victim_value.getDefiningOp(); ASSERT(def_op); builder.setInsertionPointAfter(def_op); - builder.create( - def_op->getLoc(), name_attr, victim_value); + mlir::emitpybytecode::StoreFastOp::create( + builder, def_op->getLoc(), name_attr, victim_value); // Before each original use: LOAD_FAST to reload the spilled value for (auto *use : uses) { auto *user_op = use->getOwner(); builder.setInsertionPoint(user_op); - auto reload = builder.create( - user_op->getLoc(), victim_value.getType(), name_attr); + auto reload = mlir::emitpybytecode::LoadFastOp::create( + builder, user_op->getLoc(), victim_value.getType(), name_attr); use->set(reload.getOutput()); } } @@ -254,14 +254,14 @@ class LinearScanRegisterAllocation // Insert STORE_FAST at the very start of the block builder.setInsertionPoint(bb, bb->begin()); - builder.create(loc, name_attr, arg); + mlir::emitpybytecode::StoreFastOp::create(builder, loc, name_attr, arg); // Before each original use: LOAD_FAST to reload the value for (auto *use : uses) { auto *user_op = use->getOwner(); builder.setInsertionPoint(user_op); - auto reload = builder.create( - user_op->getLoc(), arg.getType(), name_attr); + auto reload = mlir::emitpybytecode::LoadFastOp::create( + builder, user_op->getLoc(), arg.getType(), name_attr); use->set(reload.getOutput()); } } @@ -358,7 +358,7 @@ class LinearScanRegisterAllocation ASSERT(std::holds_alternative(to_spill->value)); auto [op_ptr, idx] = std::get(to_spill->value); auto for_iter = mlir::cast(op_ptr); - auto body_arg = for_iter.getBody()->getArgument(idx); + auto body_arg = for_iter.getBody()->getArgument(static_cast(idx)); do_spill_block_argument(body_arg, name_attr, builder); } @@ -377,8 +377,8 @@ class LinearScanRegisterAllocation */ void preallocate_r0_clobbering_operations( std::span unhandled, - const LiveIntervalAnalysis &live_interval_analysis, - LiveIntervalSet &inactive) + const LiveIntervalAnalysis &, + LiveIntervalSet &) { auto logger = get_regalloc_logger(); @@ -642,10 +642,10 @@ class LinearScanRegisterAllocation // Insert: push r{cur_reg}, move r{scratch}, r{cur_reg}, pop r{cur_reg} builder.setInsertionPoint(current_value.getDefiningOp()); - builder.create(loc, cur_reg); + mlir::emitpybytecode::Push::create(builder, loc, cur_reg); builder.setInsertionPointAfter(current_value.getDefiningOp()); - builder.create(loc, *scratch_reg, cur_reg); - builder.create(loc, cur_reg); + mlir::emitpybytecode::Move::create(builder, loc, *scratch_reg, cur_reg); + mlir::emitpybytecode::Pop::create(builder, loc, cur_reg); value2mem_map.insert_or_assign(cur.value, Reg{ .idx = *scratch_reg }); @@ -672,10 +672,10 @@ class LinearScanRegisterAllocation // Save cur_reg before FOR_ITER, move loop variable to scratch, restore cur_reg builder.setInsertionPoint(for_iter_op); - builder.create(loc, cur_reg); + mlir::emitpybytecode::Push::create(builder, loc, cur_reg); builder.setInsertionPoint(body_block, body_block->begin()); - builder.create(loc, *scratch_reg, cur_reg); - builder.create(loc, cur_reg); + mlir::emitpybytecode::Move::create(builder, loc, *scratch_reg, cur_reg); + mlir::emitpybytecode::Pop::create(builder, loc, cur_reg); value2mem_map.insert_or_assign(cur.value, Reg{ .idx = *scratch_reg }); free.set(*scratch_reg, false); diff --git a/src/executable/mlir/Target/PythonBytecode/TranslateToPythonBytecode.cpp b/src/executable/mlir/Target/PythonBytecode/TranslateToPythonBytecode.cpp index 00a1398b..fe1a4dfa 100644 --- a/src/executable/mlir/Target/PythonBytecode/TranslateToPythonBytecode.cpp +++ b/src/executable/mlir/Target/PythonBytecode/TranslateToPythonBytecode.cpp @@ -299,7 +299,7 @@ struct PythonBytecodeEmitter return ::py::NameConstant{ ::py::NoneType{} }; }); return value; - }; + } Register get_register(const mlir::Value &value) const { @@ -308,7 +308,7 @@ struct PythonBytecodeEmitter const auto reg = std::get(mem).idx; ASSERT(reg <= std::numeric_limits::max()); - return reg; + return static_cast(reg); } Register get_register(mlir::Operation *producing_operation, @@ -320,7 +320,7 @@ struct PythonBytecodeEmitter const auto reg = std::get(mem).idx; ASSERT(reg <= std::numeric_limits::max()); - return reg; + return static_cast(reg); } Register get_name_idx(StringRef name) const @@ -335,7 +335,7 @@ struct PythonBytecodeEmitter ASSERT(it != names_array.end()); const auto idx = std::distance(names_array.begin(), it); ASSERT(idx <= std::numeric_limits::max()); - return idx; + return static_cast(idx); } Register get_local_idx(StringRef name) const @@ -352,7 +352,7 @@ struct PythonBytecodeEmitter return std::find(cellvars.begin(), cellvars.end(), varname) == cellvars.end(); }); ASSERT(idx <= std::numeric_limits::max()); - return idx; + return static_cast(idx); } void enter_function_op(mlir::func::FuncOp op) @@ -1035,7 +1035,7 @@ LogicalResult PythonBytecodeEmitter::emitOperation(mlir::emitpybytecode::RaiseVa return success(); } -template<> LogicalResult PythonBytecodeEmitter::emitOperation(mlir::emitpybytecode::ReRaiseOp &op) +template<> LogicalResult PythonBytecodeEmitter::emitOperation(mlir::emitpybytecode::ReRaiseOp &) { emit(); return success(); @@ -1185,7 +1185,9 @@ LogicalResult PythonBytecodeEmitter::emitOperation( for (auto [keyword, value] : llvm::zip(op.getKeywords().getValues(), op.getKwargs())) { kwarg_registers.push_back(get_register(value)); - keywords_registers.push_back(add_name(keyword)); + const auto name_idx = add_name(keyword); + ASSERT(name_idx <= std::numeric_limits::max()); + keywords_registers.push_back(static_cast(name_idx)); } emit(get_register(op.getCallee()), @@ -1258,7 +1260,7 @@ template<> LogicalResult PythonBytecodeEmitter::emitOperation(mlir::emitpybyteco { const auto src = op.getSrc(); ASSERT(src <= std::numeric_limits::max()); - push(src); + push(static_cast(src)); return success(); } diff --git a/src/executable/mlir/Target/test/PythonBytecode/CMakeLists.txt b/src/executable/mlir/Target/test/PythonBytecode/CMakeLists.txt index ed10b78b..a86679e8 100644 --- a/src/executable/mlir/Target/test/PythonBytecode/CMakeLists.txt +++ b/src/executable/mlir/Target/test/PythonBytecode/CMakeLists.txt @@ -8,4 +8,5 @@ add_executable(python_bytecode_tests target_link_libraries(python_bytecode_tests PRIVATE TargetPythonBytecode gtest_main) +python_cpp_link_project_options(python_bytecode_tests) gtest_discover_tests(python_bytecode_tests) \ No newline at end of file diff --git a/src/executable/mlir/Target/test/PythonBytecode/LiveAnalysis_tests.cpp b/src/executable/mlir/Target/test/PythonBytecode/LiveAnalysis_tests.cpp index 2e695d9c..fe8a34c9 100644 --- a/src/executable/mlir/Target/test/PythonBytecode/LiveAnalysis_tests.cpp +++ b/src/executable/mlir/Target/test/PythonBytecode/LiveAnalysis_tests.cpp @@ -145,7 +145,6 @@ TEST_F(LiveAnalysisTest, LiveAnalysis) // Test 4: Verify that block_arg_18 appears in alive_at_timestep bool found_block_arg = false; - bool found_block_arg_inputs = false; for (const auto ×tep : live_analysis.alive_at_timestep) { for (const auto &val : timestep) { if (std::holds_alternative(val)) { diff --git a/src/executable/mlir/tools/python-mlir-opt/CMakeLists.txt b/src/executable/mlir/tools/python-mlir-opt/CMakeLists.txt index dd7af4b8..6d4a8815 100644 --- a/src/executable/mlir/tools/python-mlir-opt/CMakeLists.txt +++ b/src/executable/mlir/tools/python-mlir-opt/CMakeLists.txt @@ -26,3 +26,5 @@ target_include_directories(python-mlir-opt PRIVATE ${PYTHON_MLIR_SOURCE_DIR} ${PYTHON_MLIR_BINARY_DIR} ) + +python_cpp_link_project_options(python-mlir-opt) diff --git a/src/runtime/PyCell.cpp b/src/runtime/PyCell.cpp index 91784711..55e1f820 100644 --- a/src/runtime/PyCell.cpp +++ b/src/runtime/PyCell.cpp @@ -80,7 +80,17 @@ bool PyCell::empty() const PyResult PyCell::__repr__() const { return PyString::create(to_string()); } +// GCC 16 inlines the std::variant copy-assignment behind py::Value and then +// attributes the std::vector destructor's operator delete to the stack slot +// holding the variant. Nothing is freed here; the diagnostic is a false positive. +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wfree-nonheap-object" +#endif void PyCell::set_cell(const Value &new_value) { m_content = new_value; } +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif namespace { std::once_flag cell_flag; diff --git a/src/runtime/modules/PosixModule.cpp b/src/runtime/modules/PosixModule.cpp index 8645cbf5..6532ddb1 100644 --- a/src/runtime/modules/PosixModule.cpp +++ b/src/runtime/modules/PosixModule.cpp @@ -17,6 +17,7 @@ #include #include +#include namespace fs = std::filesystem; diff --git a/src/runtime/modules/SysModule.cpp b/src/runtime/modules/SysModule.cpp index 83304b5e..44e3b890 100644 --- a/src/runtime/modules/SysModule.cpp +++ b/src/runtime/modules/SysModule.cpp @@ -40,7 +40,7 @@ PyResult create_sys_paths(Interpreter &interpreter) const auto &entry_script = interpreter.entry_script(); auto entry_parent = PyString::create(std::filesystem::path(entry_script).parent_path()); if (entry_parent.is_err()) return Err(entry_parent.unwrap_err()); - auto path_list = PyList::create({ + auto path_list = PyList::create(std::vector{ entry_parent.unwrap(), PyString::create(kPythonLibPath.data()).unwrap(), });