Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/premerge.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -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
Expand All@@ -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
Expand Down
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles)

project(python++)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 26)

include(cmake/CPM.cmake)

Expand DownExpand Up@@ -77,10 +77,26 @@ target_compile_options(project_warnings
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-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 $<$<COMPILE_LANGUAGE:CXX>:-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 <cstdint> rather than coming along with <bit>.
check_cxx_source_compiles(
"#include <bit>
#include <cstdint>
constexpr double f64v = 19880124.0;
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);"
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);
int main() { return u64v == 0; }"
STL_SUPPORTS_BIT_CAST)

find_library(MATH_LIBRARY m)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,4 +70,4 @@
}
]

}
}
36 changes: 36 additions & 0 deletions cmake/PythonCppFlags.cmake
Original file line numberDiff line numberDiff line change
@@ -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.<name>` object library
# rather than in `<name>` itself, and only forward include directories to it -
# not the usage requirements of libraries linked afterwards. Linking the flags
# to `<name>` alone would therefore silently compile nothing with them, so this
# always covers the `obj.<name>` 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()
3 changes: 2 additions & 1 deletion integration/CMakeLists.txt
Original file line numberDiff line numberDiff line change
@@ -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(
Expand Down
27 changes: 8 additions & 19 deletions src/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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}
Expand All@@ -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")
Expand All@@ -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
Expand All@@ -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)
Comment thread
gf712 marked this conversation as resolved.
# 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)

Expand All@@ -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)
10 changes: 10 additions & 0 deletions src/executable/bytecode/instructions/Instructions.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
{
Expand Down
18 changes: 18 additions & 0 deletions src/executable/mlir/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.<name>` 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)
Expand All@@ -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)
4 changes: 3 additions & 1 deletion src/executable/mlir/Conversion/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,6 @@ add_mlir_library(PythonConversionPasses

LINK_LIBS PUBLIC
${PYTHON_CONVERSION_LIBS}
)
)

python_cpp_link_project_options(PythonConversionPasses)
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,4 +27,5 @@ target_include_directories(PythonToPythonBytecode PUBLIC
${PYTHON_MLIR_BINARY_DIR}
)

target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
python_cpp_link_project_options(PythonToPythonBytecode)
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,22 +36,22 @@ namespace {
llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) {
if (to_expand) {
if (!result.has_value()) {
result = rewriter.create<mlir::emitpybytecode::BuildDict>(
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<mlir::emitpybytecode::DictUpdate>(
op.getLoc(), *result, value);
mlir::emitpybytecode::DictUpdate::create(
rewriter, op.getLoc(), *result, value);
} else {
if (!result.has_value()) {
keys.push_back(key);
values.push_back(value);
} else {
ASSERT(keys.empty());
ASSERT(values.empty());
rewriter.create<mlir::emitpybytecode::DictAdd>(
op.getLoc(), *result, key, value);
mlir::emitpybytecode::DictAdd::create(
rewriter, op.getLoc(), *result, key, value);
}
}
}
Expand DownExpand Up@@ -89,12 +89,12 @@ namespace {
llvm::ArrayRef<bool> requires_expansion)
{
auto list =
rewriter.create<mlir::emitpybytecode::BuildList>(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<mlir::emitpybytecode::ListExtend>(loc, list, el);
mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el);
} else {
rewriter.create<mlir::emitpybytecode::ListAppend>(loc, list, el);
mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el);
}
}
return list;
Expand DownExpand Up@@ -181,23 +181,23 @@ namespace {
for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) {
if (expand) {
if (!set.has_value()) {
set = rewriter.create<mlir::emitpybytecode::BuildSet>(
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<mlir::emitpybytecode::SetAdd>(
op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(
rewriter, op.getLoc(), *set, el);
}
}
elements.clear();
rewriter.create<mlir::emitpybytecode::SetUpdate>(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<mlir::emitpybytecode::SetAdd>(op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el);
}
rewriter.replaceOp(op, *set);
} else {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,8 +97,8 @@ namespace {
auto insertion_point = rewriter.getInsertionPoint();
auto *return_block = rewriter.createBlock(&op.getRegion());
auto value =
rewriter.create<mlir::py::ConstantOp>(op.getLoc(), rewriter.getNoneType());
rewriter.create<mlir::func::ReturnOp>(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;
})
Expand DownExpand Up@@ -136,9 +136,10 @@ namespace {
mlir::LogicalResult matchAndRewrite(mlir::py::YieldFromOp op,
mlir::PatternRewriter &rewriter) const final
{
auto iterator = rewriter.create<mlir::emitpybytecode::YieldFromIter>(
op.getLoc(), op.getIterable().getType(), op.getIterable());
auto value = rewriter.create<mlir::py::ConstantOp>(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<mlir::emitpybytecode::YieldFrom>(
op, iterator.getType(), iterator, value);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/premerge.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -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
Expand All@@ -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
Expand Down
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles)

project(python++)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 26)

include(cmake/CPM.cmake)

Expand DownExpand Up@@ -77,10 +77,26 @@ target_compile_options(project_warnings
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-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 $<$<COMPILE_LANGUAGE:CXX>:-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 <cstdint> rather than coming along with <bit>.
check_cxx_source_compiles(
"#include <bit>
#include <cstdint>
constexpr double f64v = 19880124.0;
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);"
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);
int main() { return u64v == 0; }"
STL_SUPPORTS_BIT_CAST)

find_library(MATH_LIBRARY m)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,4 +70,4 @@
}
]

}
}
36 changes: 36 additions & 0 deletions cmake/PythonCppFlags.cmake
Original file line numberDiff line numberDiff line change
@@ -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.<name>` object library
# rather than in `<name>` itself, and only forward include directories to it -
# not the usage requirements of libraries linked afterwards. Linking the flags
# to `<name>` alone would therefore silently compile nothing with them, so this
# always covers the `obj.<name>` 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()
3 changes: 2 additions & 1 deletion integration/CMakeLists.txt
Original file line numberDiff line numberDiff line change
@@ -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(
Expand Down
27 changes: 8 additions & 19 deletions src/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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}
Expand All@@ -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")
Expand All@@ -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
Expand All@@ -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)
Comment thread
gf712 marked this conversation as resolved.
# 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)

Expand All@@ -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)
10 changes: 10 additions & 0 deletions src/executable/bytecode/instructions/Instructions.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
{
Expand Down
18 changes: 18 additions & 0 deletions src/executable/mlir/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.<name>` 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)
Expand All@@ -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)
4 changes: 3 additions & 1 deletion src/executable/mlir/Conversion/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,6 @@ add_mlir_library(PythonConversionPasses

LINK_LIBS PUBLIC
${PYTHON_CONVERSION_LIBS}
)
)

python_cpp_link_project_options(PythonConversionPasses)
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,4 +27,5 @@ target_include_directories(PythonToPythonBytecode PUBLIC
${PYTHON_MLIR_BINARY_DIR}
)

target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
python_cpp_link_project_options(PythonToPythonBytecode)
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,22 +36,22 @@ namespace {
llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) {
if (to_expand) {
if (!result.has_value()) {
result = rewriter.create<mlir::emitpybytecode::BuildDict>(
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<mlir::emitpybytecode::DictUpdate>(
op.getLoc(), *result, value);
mlir::emitpybytecode::DictUpdate::create(
rewriter, op.getLoc(), *result, value);
} else {
if (!result.has_value()) {
keys.push_back(key);
values.push_back(value);
} else {
ASSERT(keys.empty());
ASSERT(values.empty());
rewriter.create<mlir::emitpybytecode::DictAdd>(
op.getLoc(), *result, key, value);
mlir::emitpybytecode::DictAdd::create(
rewriter, op.getLoc(), *result, key, value);
}
}
}
Expand DownExpand Up@@ -89,12 +89,12 @@ namespace {
llvm::ArrayRef<bool> requires_expansion)
{
auto list =
rewriter.create<mlir::emitpybytecode::BuildList>(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<mlir::emitpybytecode::ListExtend>(loc, list, el);
mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el);
} else {
rewriter.create<mlir::emitpybytecode::ListAppend>(loc, list, el);
mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el);
}
}
return list;
Expand DownExpand Up@@ -181,23 +181,23 @@ namespace {
for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) {
if (expand) {
if (!set.has_value()) {
set = rewriter.create<mlir::emitpybytecode::BuildSet>(
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<mlir::emitpybytecode::SetAdd>(
op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(
rewriter, op.getLoc(), *set, el);
}
}
elements.clear();
rewriter.create<mlir::emitpybytecode::SetUpdate>(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<mlir::emitpybytecode::SetAdd>(op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el);
}
rewriter.replaceOp(op, *set);
} else {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,8 +97,8 @@ namespace {
auto insertion_point = rewriter.getInsertionPoint();
auto *return_block = rewriter.createBlock(&op.getRegion());
auto value =
rewriter.create<mlir::py::ConstantOp>(op.getLoc(), rewriter.getNoneType());
rewriter.create<mlir::func::ReturnOp>(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;
})
Expand DownExpand Up@@ -136,9 +136,10 @@ namespace {
mlir::LogicalResult matchAndRewrite(mlir::py::YieldFromOp op,
mlir::PatternRewriter &rewriter) const final
{
auto iterator = rewriter.create<mlir::emitpybytecode::YieldFromIter>(
op.getLoc(), op.getIterable().getType(), op.getIterable());
auto value = rewriter.create<mlir::py::ConstantOp>(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<mlir::emitpybytecode::YieldFrom>(
op, iterator.getType(), iterator, value);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/premerge.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -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
Expand All@@ -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
Expand Down
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles)

project(python++)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 26)

include(cmake/CPM.cmake)

Expand DownExpand Up@@ -77,10 +77,26 @@ target_compile_options(project_warnings
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-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 $<$<COMPILE_LANGUAGE:CXX>:-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 <cstdint> rather than coming along with <bit>.
check_cxx_source_compiles(
"#include <bit>
#include <cstdint>
constexpr double f64v = 19880124.0;
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);"
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);
int main() { return u64v == 0; }"
STL_SUPPORTS_BIT_CAST)

find_library(MATH_LIBRARY m)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,4 +70,4 @@
}
]

}
}
36 changes: 36 additions & 0 deletions cmake/PythonCppFlags.cmake
Original file line numberDiff line numberDiff line change
@@ -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.<name>` object library
# rather than in `<name>` itself, and only forward include directories to it -
# not the usage requirements of libraries linked afterwards. Linking the flags
# to `<name>` alone would therefore silently compile nothing with them, so this
# always covers the `obj.<name>` 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()
3 changes: 2 additions & 1 deletion integration/CMakeLists.txt
Original file line numberDiff line numberDiff line change
@@ -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(
Expand Down
27 changes: 8 additions & 19 deletions src/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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}
Expand All@@ -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")
Expand All@@ -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
Expand All@@ -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)
Comment thread
gf712 marked this conversation as resolved.
# 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)

Expand All@@ -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)
10 changes: 10 additions & 0 deletions src/executable/bytecode/instructions/Instructions.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
{
Expand Down
18 changes: 18 additions & 0 deletions src/executable/mlir/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.<name>` 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)
Expand All@@ -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)
4 changes: 3 additions & 1 deletion src/executable/mlir/Conversion/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,6 @@ add_mlir_library(PythonConversionPasses

LINK_LIBS PUBLIC
${PYTHON_CONVERSION_LIBS}
)
)

python_cpp_link_project_options(PythonConversionPasses)
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,4 +27,5 @@ target_include_directories(PythonToPythonBytecode PUBLIC
${PYTHON_MLIR_BINARY_DIR}
)

target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
python_cpp_link_project_options(PythonToPythonBytecode)
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,22 +36,22 @@ namespace {
llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) {
if (to_expand) {
if (!result.has_value()) {
result = rewriter.create<mlir::emitpybytecode::BuildDict>(
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<mlir::emitpybytecode::DictUpdate>(
op.getLoc(), *result, value);
mlir::emitpybytecode::DictUpdate::create(
rewriter, op.getLoc(), *result, value);
} else {
if (!result.has_value()) {
keys.push_back(key);
values.push_back(value);
} else {
ASSERT(keys.empty());
ASSERT(values.empty());
rewriter.create<mlir::emitpybytecode::DictAdd>(
op.getLoc(), *result, key, value);
mlir::emitpybytecode::DictAdd::create(
rewriter, op.getLoc(), *result, key, value);
}
}
}
Expand DownExpand Up@@ -89,12 +89,12 @@ namespace {
llvm::ArrayRef<bool> requires_expansion)
{
auto list =
rewriter.create<mlir::emitpybytecode::BuildList>(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<mlir::emitpybytecode::ListExtend>(loc, list, el);
mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el);
} else {
rewriter.create<mlir::emitpybytecode::ListAppend>(loc, list, el);
mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el);
}
}
return list;
Expand DownExpand Up@@ -181,23 +181,23 @@ namespace {
for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) {
if (expand) {
if (!set.has_value()) {
set = rewriter.create<mlir::emitpybytecode::BuildSet>(
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<mlir::emitpybytecode::SetAdd>(
op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(
rewriter, op.getLoc(), *set, el);
}
}
elements.clear();
rewriter.create<mlir::emitpybytecode::SetUpdate>(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<mlir::emitpybytecode::SetAdd>(op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el);
}
rewriter.replaceOp(op, *set);
} else {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,8 +97,8 @@ namespace {
auto insertion_point = rewriter.getInsertionPoint();
auto *return_block = rewriter.createBlock(&op.getRegion());
auto value =
rewriter.create<mlir::py::ConstantOp>(op.getLoc(), rewriter.getNoneType());
rewriter.create<mlir::func::ReturnOp>(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;
})
Expand DownExpand Up@@ -136,9 +136,10 @@ namespace {
mlir::LogicalResult matchAndRewrite(mlir::py::YieldFromOp op,
mlir::PatternRewriter &rewriter) const final
{
auto iterator = rewriter.create<mlir::emitpybytecode::YieldFromIter>(
op.getLoc(), op.getIterable().getType(), op.getIterable());
auto value = rewriter.create<mlir::py::ConstantOp>(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<mlir::emitpybytecode::YieldFrom>(
op, iterator.getType(), iterator, value);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/premerge.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -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
Expand All@@ -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
Expand Down
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles)

project(python++)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 26)

include(cmake/CPM.cmake)

Expand DownExpand Up@@ -77,10 +77,26 @@ target_compile_options(project_warnings
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-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 $<$<COMPILE_LANGUAGE:CXX>:-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 <cstdint> rather than coming along with <bit>.
check_cxx_source_compiles(
"#include <bit>
#include <cstdint>
constexpr double f64v = 19880124.0;
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);"
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);
int main() { return u64v == 0; }"
STL_SUPPORTS_BIT_CAST)

find_library(MATH_LIBRARY m)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,4 +70,4 @@
}
]

}
}
36 changes: 36 additions & 0 deletions cmake/PythonCppFlags.cmake
Original file line numberDiff line numberDiff line change
@@ -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.<name>` object library
# rather than in `<name>` itself, and only forward include directories to it -
# not the usage requirements of libraries linked afterwards. Linking the flags
# to `<name>` alone would therefore silently compile nothing with them, so this
# always covers the `obj.<name>` 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()
3 changes: 2 additions & 1 deletion integration/CMakeLists.txt
Original file line numberDiff line numberDiff line change
@@ -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(
Expand Down
27 changes: 8 additions & 19 deletions src/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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}
Expand All@@ -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")
Expand All@@ -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
Expand All@@ -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)
Comment thread
gf712 marked this conversation as resolved.
# 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)

Expand All@@ -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)
10 changes: 10 additions & 0 deletions src/executable/bytecode/instructions/Instructions.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
{
Expand Down
18 changes: 18 additions & 0 deletions src/executable/mlir/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.<name>` 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)
Expand All@@ -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)
4 changes: 3 additions & 1 deletion src/executable/mlir/Conversion/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,6 @@ add_mlir_library(PythonConversionPasses

LINK_LIBS PUBLIC
${PYTHON_CONVERSION_LIBS}
)
)

python_cpp_link_project_options(PythonConversionPasses)
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,4 +27,5 @@ target_include_directories(PythonToPythonBytecode PUBLIC
${PYTHON_MLIR_BINARY_DIR}
)

target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
python_cpp_link_project_options(PythonToPythonBytecode)
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,22 +36,22 @@ namespace {
llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) {
if (to_expand) {
if (!result.has_value()) {
result = rewriter.create<mlir::emitpybytecode::BuildDict>(
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<mlir::emitpybytecode::DictUpdate>(
op.getLoc(), *result, value);
mlir::emitpybytecode::DictUpdate::create(
rewriter, op.getLoc(), *result, value);
} else {
if (!result.has_value()) {
keys.push_back(key);
values.push_back(value);
} else {
ASSERT(keys.empty());
ASSERT(values.empty());
rewriter.create<mlir::emitpybytecode::DictAdd>(
op.getLoc(), *result, key, value);
mlir::emitpybytecode::DictAdd::create(
rewriter, op.getLoc(), *result, key, value);
}
}
}
Expand DownExpand Up@@ -89,12 +89,12 @@ namespace {
llvm::ArrayRef<bool> requires_expansion)
{
auto list =
rewriter.create<mlir::emitpybytecode::BuildList>(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<mlir::emitpybytecode::ListExtend>(loc, list, el);
mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el);
} else {
rewriter.create<mlir::emitpybytecode::ListAppend>(loc, list, el);
mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el);
}
}
return list;
Expand DownExpand Up@@ -181,23 +181,23 @@ namespace {
for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) {
if (expand) {
if (!set.has_value()) {
set = rewriter.create<mlir::emitpybytecode::BuildSet>(
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<mlir::emitpybytecode::SetAdd>(
op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(
rewriter, op.getLoc(), *set, el);
}
}
elements.clear();
rewriter.create<mlir::emitpybytecode::SetUpdate>(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<mlir::emitpybytecode::SetAdd>(op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el);
}
rewriter.replaceOp(op, *set);
} else {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,8 +97,8 @@ namespace {
auto insertion_point = rewriter.getInsertionPoint();
auto *return_block = rewriter.createBlock(&op.getRegion());
auto value =
rewriter.create<mlir::py::ConstantOp>(op.getLoc(), rewriter.getNoneType());
rewriter.create<mlir::func::ReturnOp>(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;
})
Expand DownExpand Up@@ -136,9 +136,10 @@ namespace {
mlir::LogicalResult matchAndRewrite(mlir::py::YieldFromOp op,
mlir::PatternRewriter &rewriter) const final
{
auto iterator = rewriter.create<mlir::emitpybytecode::YieldFromIter>(
op.getLoc(), op.getIterable().getType(), op.getIterable());
auto value = rewriter.create<mlir::py::ConstantOp>(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<mlir::emitpybytecode::YieldFrom>(
op, iterator.getType(), iterator, value);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/premerge.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -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
Expand All@@ -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
Expand Down
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles)

project(python++)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 26)

include(cmake/CPM.cmake)

Expand DownExpand Up@@ -77,10 +77,26 @@ target_compile_options(project_warnings
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-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 $<$<COMPILE_LANGUAGE:CXX>:-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 <cstdint> rather than coming along with <bit>.
check_cxx_source_compiles(
"#include <bit>
#include <cstdint>
constexpr double f64v = 19880124.0;
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);"
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);
int main() { return u64v == 0; }"
STL_SUPPORTS_BIT_CAST)

find_library(MATH_LIBRARY m)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,4 +70,4 @@
}
]

}
}
36 changes: 36 additions & 0 deletions cmake/PythonCppFlags.cmake
Original file line numberDiff line numberDiff line change
@@ -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.<name>` object library
# rather than in `<name>` itself, and only forward include directories to it -
# not the usage requirements of libraries linked afterwards. Linking the flags
# to `<name>` alone would therefore silently compile nothing with them, so this
# always covers the `obj.<name>` 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()
3 changes: 2 additions & 1 deletion integration/CMakeLists.txt
Original file line numberDiff line numberDiff line change
@@ -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(
Expand Down
27 changes: 8 additions & 19 deletions src/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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}
Expand All@@ -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")
Expand All@@ -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
Expand All@@ -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)
Comment thread
gf712 marked this conversation as resolved.
# 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)

Expand All@@ -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)
10 changes: 10 additions & 0 deletions src/executable/bytecode/instructions/Instructions.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
{
Expand Down
18 changes: 18 additions & 0 deletions src/executable/mlir/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.<name>` 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)
Expand All@@ -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)
4 changes: 3 additions & 1 deletion src/executable/mlir/Conversion/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,6 @@ add_mlir_library(PythonConversionPasses

LINK_LIBS PUBLIC
${PYTHON_CONVERSION_LIBS}
)
)

python_cpp_link_project_options(PythonConversionPasses)
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,4 +27,5 @@ target_include_directories(PythonToPythonBytecode PUBLIC
${PYTHON_MLIR_BINARY_DIR}
)

target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
python_cpp_link_project_options(PythonToPythonBytecode)
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,22 +36,22 @@ namespace {
llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) {
if (to_expand) {
if (!result.has_value()) {
result = rewriter.create<mlir::emitpybytecode::BuildDict>(
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<mlir::emitpybytecode::DictUpdate>(
op.getLoc(), *result, value);
mlir::emitpybytecode::DictUpdate::create(
rewriter, op.getLoc(), *result, value);
} else {
if (!result.has_value()) {
keys.push_back(key);
values.push_back(value);
} else {
ASSERT(keys.empty());
ASSERT(values.empty());
rewriter.create<mlir::emitpybytecode::DictAdd>(
op.getLoc(), *result, key, value);
mlir::emitpybytecode::DictAdd::create(
rewriter, op.getLoc(), *result, key, value);
}
}
}
Expand DownExpand Up@@ -89,12 +89,12 @@ namespace {
llvm::ArrayRef<bool> requires_expansion)
{
auto list =
rewriter.create<mlir::emitpybytecode::BuildList>(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<mlir::emitpybytecode::ListExtend>(loc, list, el);
mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el);
} else {
rewriter.create<mlir::emitpybytecode::ListAppend>(loc, list, el);
mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el);
}
}
return list;
Expand DownExpand Up@@ -181,23 +181,23 @@ namespace {
for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) {
if (expand) {
if (!set.has_value()) {
set = rewriter.create<mlir::emitpybytecode::BuildSet>(
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<mlir::emitpybytecode::SetAdd>(
op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(
rewriter, op.getLoc(), *set, el);
}
}
elements.clear();
rewriter.create<mlir::emitpybytecode::SetUpdate>(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<mlir::emitpybytecode::SetAdd>(op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el);
}
rewriter.replaceOp(op, *set);
} else {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,8 +97,8 @@ namespace {
auto insertion_point = rewriter.getInsertionPoint();
auto *return_block = rewriter.createBlock(&op.getRegion());
auto value =
rewriter.create<mlir::py::ConstantOp>(op.getLoc(), rewriter.getNoneType());
rewriter.create<mlir::func::ReturnOp>(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;
})
Expand DownExpand Up@@ -136,9 +136,10 @@ namespace {
mlir::LogicalResult matchAndRewrite(mlir::py::YieldFromOp op,
mlir::PatternRewriter &rewriter) const final
{
auto iterator = rewriter.create<mlir::emitpybytecode::YieldFromIter>(
op.getLoc(), op.getIterable().getType(), op.getIterable());
auto value = rewriter.create<mlir::py::ConstantOp>(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<mlir::emitpybytecode::YieldFrom>(
op, iterator.getType(), iterator, value);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/premerge.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -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
Expand All@@ -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
Expand Down
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles)

project(python++)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 26)

include(cmake/CPM.cmake)

Expand DownExpand Up@@ -77,10 +77,26 @@ target_compile_options(project_warnings
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-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 $<$<COMPILE_LANGUAGE:CXX>:-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 <cstdint> rather than coming along with <bit>.
check_cxx_source_compiles(
"#include <bit>
#include <cstdint>
constexpr double f64v = 19880124.0;
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);"
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);
int main() { return u64v == 0; }"
STL_SUPPORTS_BIT_CAST)

find_library(MATH_LIBRARY m)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,4 +70,4 @@
}
]

}
}
36 changes: 36 additions & 0 deletions cmake/PythonCppFlags.cmake
Original file line numberDiff line numberDiff line change
@@ -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.<name>` object library
# rather than in `<name>` itself, and only forward include directories to it -
# not the usage requirements of libraries linked afterwards. Linking the flags
# to `<name>` alone would therefore silently compile nothing with them, so this
# always covers the `obj.<name>` 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()
3 changes: 2 additions & 1 deletion integration/CMakeLists.txt
Original file line numberDiff line numberDiff line change
@@ -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(
Expand Down
27 changes: 8 additions & 19 deletions src/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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}
Expand All@@ -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")
Expand All@@ -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
Expand All@@ -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)
Comment thread
gf712 marked this conversation as resolved.
# 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)

Expand All@@ -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)
10 changes: 10 additions & 0 deletions src/executable/bytecode/instructions/Instructions.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
{
Expand Down
18 changes: 18 additions & 0 deletions src/executable/mlir/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.<name>` 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)
Expand All@@ -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)
4 changes: 3 additions & 1 deletion src/executable/mlir/Conversion/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,6 @@ add_mlir_library(PythonConversionPasses

LINK_LIBS PUBLIC
${PYTHON_CONVERSION_LIBS}
)
)

python_cpp_link_project_options(PythonConversionPasses)
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,4 +27,5 @@ target_include_directories(PythonToPythonBytecode PUBLIC
${PYTHON_MLIR_BINARY_DIR}
)

target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
python_cpp_link_project_options(PythonToPythonBytecode)
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,22 +36,22 @@ namespace {
llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) {
if (to_expand) {
if (!result.has_value()) {
result = rewriter.create<mlir::emitpybytecode::BuildDict>(
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<mlir::emitpybytecode::DictUpdate>(
op.getLoc(), *result, value);
mlir::emitpybytecode::DictUpdate::create(
rewriter, op.getLoc(), *result, value);
} else {
if (!result.has_value()) {
keys.push_back(key);
values.push_back(value);
} else {
ASSERT(keys.empty());
ASSERT(values.empty());
rewriter.create<mlir::emitpybytecode::DictAdd>(
op.getLoc(), *result, key, value);
mlir::emitpybytecode::DictAdd::create(
rewriter, op.getLoc(), *result, key, value);
}
}
}
Expand DownExpand Up@@ -89,12 +89,12 @@ namespace {
llvm::ArrayRef<bool> requires_expansion)
{
auto list =
rewriter.create<mlir::emitpybytecode::BuildList>(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<mlir::emitpybytecode::ListExtend>(loc, list, el);
mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el);
} else {
rewriter.create<mlir::emitpybytecode::ListAppend>(loc, list, el);
mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el);
}
}
return list;
Expand DownExpand Up@@ -181,23 +181,23 @@ namespace {
for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) {
if (expand) {
if (!set.has_value()) {
set = rewriter.create<mlir::emitpybytecode::BuildSet>(
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<mlir::emitpybytecode::SetAdd>(
op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(
rewriter, op.getLoc(), *set, el);
}
}
elements.clear();
rewriter.create<mlir::emitpybytecode::SetUpdate>(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<mlir::emitpybytecode::SetAdd>(op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el);
}
rewriter.replaceOp(op, *set);
} else {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,8 +97,8 @@ namespace {
auto insertion_point = rewriter.getInsertionPoint();
auto *return_block = rewriter.createBlock(&op.getRegion());
auto value =
rewriter.create<mlir::py::ConstantOp>(op.getLoc(), rewriter.getNoneType());
rewriter.create<mlir::func::ReturnOp>(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;
})
Expand DownExpand Up@@ -136,9 +136,10 @@ namespace {
mlir::LogicalResult matchAndRewrite(mlir::py::YieldFromOp op,
mlir::PatternRewriter &rewriter) const final
{
auto iterator = rewriter.create<mlir::emitpybytecode::YieldFromIter>(
op.getLoc(), op.getIterable().getType(), op.getIterable());
auto value = rewriter.create<mlir::py::ConstantOp>(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<mlir::emitpybytecode::YieldFrom>(
op, iterator.getType(), iterator, value);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/premerge.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -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
Expand All@@ -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
Expand Down
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles)

project(python++)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 26)

include(cmake/CPM.cmake)

Expand DownExpand Up@@ -77,10 +77,26 @@ target_compile_options(project_warnings
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-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 $<$<COMPILE_LANGUAGE:CXX>:-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 <cstdint> rather than coming along with <bit>.
check_cxx_source_compiles(
"#include <bit>
#include <cstdint>
constexpr double f64v = 19880124.0;
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);"
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);
int main() { return u64v == 0; }"
STL_SUPPORTS_BIT_CAST)

find_library(MATH_LIBRARY m)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,4 +70,4 @@
}
]

}
}
36 changes: 36 additions & 0 deletions cmake/PythonCppFlags.cmake
Original file line numberDiff line numberDiff line change
@@ -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.<name>` object library
# rather than in `<name>` itself, and only forward include directories to it -
# not the usage requirements of libraries linked afterwards. Linking the flags
# to `<name>` alone would therefore silently compile nothing with them, so this
# always covers the `obj.<name>` 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()
3 changes: 2 additions & 1 deletion integration/CMakeLists.txt
Original file line numberDiff line numberDiff line change
@@ -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(
Expand Down
27 changes: 8 additions & 19 deletions src/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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}
Expand All@@ -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")
Expand All@@ -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
Expand All@@ -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)
Comment thread
gf712 marked this conversation as resolved.
# 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)

Expand All@@ -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)
10 changes: 10 additions & 0 deletions src/executable/bytecode/instructions/Instructions.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
{
Expand Down
18 changes: 18 additions & 0 deletions src/executable/mlir/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.<name>` 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)
Expand All@@ -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)
4 changes: 3 additions & 1 deletion src/executable/mlir/Conversion/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,6 @@ add_mlir_library(PythonConversionPasses

LINK_LIBS PUBLIC
${PYTHON_CONVERSION_LIBS}
)
)

python_cpp_link_project_options(PythonConversionPasses)
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,4 +27,5 @@ target_include_directories(PythonToPythonBytecode PUBLIC
${PYTHON_MLIR_BINARY_DIR}
)

target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
python_cpp_link_project_options(PythonToPythonBytecode)
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,22 +36,22 @@ namespace {
llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) {
if (to_expand) {
if (!result.has_value()) {
result = rewriter.create<mlir::emitpybytecode::BuildDict>(
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<mlir::emitpybytecode::DictUpdate>(
op.getLoc(), *result, value);
mlir::emitpybytecode::DictUpdate::create(
rewriter, op.getLoc(), *result, value);
} else {
if (!result.has_value()) {
keys.push_back(key);
values.push_back(value);
} else {
ASSERT(keys.empty());
ASSERT(values.empty());
rewriter.create<mlir::emitpybytecode::DictAdd>(
op.getLoc(), *result, key, value);
mlir::emitpybytecode::DictAdd::create(
rewriter, op.getLoc(), *result, key, value);
}
}
}
Expand DownExpand Up@@ -89,12 +89,12 @@ namespace {
llvm::ArrayRef<bool> requires_expansion)
{
auto list =
rewriter.create<mlir::emitpybytecode::BuildList>(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<mlir::emitpybytecode::ListExtend>(loc, list, el);
mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el);
} else {
rewriter.create<mlir::emitpybytecode::ListAppend>(loc, list, el);
mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el);
}
}
return list;
Expand DownExpand Up@@ -181,23 +181,23 @@ namespace {
for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) {
if (expand) {
if (!set.has_value()) {
set = rewriter.create<mlir::emitpybytecode::BuildSet>(
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<mlir::emitpybytecode::SetAdd>(
op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(
rewriter, op.getLoc(), *set, el);
}
}
elements.clear();
rewriter.create<mlir::emitpybytecode::SetUpdate>(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<mlir::emitpybytecode::SetAdd>(op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el);
}
rewriter.replaceOp(op, *set);
} else {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,8 +97,8 @@ namespace {
auto insertion_point = rewriter.getInsertionPoint();
auto *return_block = rewriter.createBlock(&op.getRegion());
auto value =
rewriter.create<mlir::py::ConstantOp>(op.getLoc(), rewriter.getNoneType());
rewriter.create<mlir::func::ReturnOp>(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;
})
Expand DownExpand Up@@ -136,9 +136,10 @@ namespace {
mlir::LogicalResult matchAndRewrite(mlir::py::YieldFromOp op,
mlir::PatternRewriter &rewriter) const final
{
auto iterator = rewriter.create<mlir::emitpybytecode::YieldFromIter>(
op.getLoc(), op.getIterable().getType(), op.getIterable());
auto value = rewriter.create<mlir::py::ConstantOp>(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<mlir::emitpybytecode::YieldFrom>(
op, iterator.getType(), iterator, value);
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions .github/workflows/premerge.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -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
Expand All@@ -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
Expand Down
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,7 +5,7 @@ include(CheckCXXSourceCompiles)

project(python++)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 26)

include(cmake/CPM.cmake)

Expand DownExpand Up@@ -77,10 +77,26 @@ target_compile_options(project_warnings
INTERFACE $<$<COMPILE_LANGUAGE:CXX>:-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 $<$<COMPILE_LANGUAGE:CXX>:-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 <cstdint> rather than coming along with <bit>.
check_cxx_source_compiles(
"#include <bit>
#include <cstdint>
constexpr double f64v = 19880124.0;
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);"
constexpr auto u64v = std::bit_cast<std::uint64_t>(f64v);
int main() { return u64v == 0; }"
STL_SUPPORTS_BIT_CAST)

find_library(MATH_LIBRARY m)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line numberDiff line numberDiff line change
Expand Up@@ -70,4 +70,4 @@
}
]

}
}
36 changes: 36 additions & 0 deletions cmake/PythonCppFlags.cmake
Original file line numberDiff line numberDiff line change
@@ -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.<name>` object library
# rather than in `<name>` itself, and only forward include directories to it -
# not the usage requirements of libraries linked afterwards. Linking the flags
# to `<name>` alone would therefore silently compile nothing with them, so this
# always covers the `obj.<name>` 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()
3 changes: 2 additions & 1 deletion integration/CMakeLists.txt
Original file line numberDiff line numberDiff line change
@@ -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(
Expand Down
27 changes: 8 additions & 19 deletions src/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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}
Expand All@@ -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")
Expand All@@ -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
Expand All@@ -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)
Comment thread
gf712 marked this conversation as resolved.
# 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)

Expand All@@ -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)
10 changes: 10 additions & 0 deletions src/executable/bytecode/instructions/Instructions.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
{
Expand Down
18 changes: 18 additions & 0 deletions src/executable/mlir/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.<name>` 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)
Expand All@@ -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)
4 changes: 3 additions & 1 deletion src/executable/mlir/Conversion/CMakeLists.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,4 +15,6 @@ add_mlir_library(PythonConversionPasses

LINK_LIBS PUBLIC
${PYTHON_CONVERSION_LIBS}
)
)

python_cpp_link_project_options(PythonConversionPasses)
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,4 +27,5 @@ target_include_directories(PythonToPythonBytecode PUBLIC
${PYTHON_MLIR_BINARY_DIR}
)

target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
target_link_libraries(PythonToPythonBytecode PRIVATE spdlog)
python_cpp_link_project_options(PythonToPythonBytecode)
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,22 +36,22 @@ namespace {
llvm::zip(op.getKeys(), op.getValues(), op.getRequiresExpansion())) {
if (to_expand) {
if (!result.has_value()) {
result = rewriter.create<mlir::emitpybytecode::BuildDict>(
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<mlir::emitpybytecode::DictUpdate>(
op.getLoc(), *result, value);
mlir::emitpybytecode::DictUpdate::create(
rewriter, op.getLoc(), *result, value);
} else {
if (!result.has_value()) {
keys.push_back(key);
values.push_back(value);
} else {
ASSERT(keys.empty());
ASSERT(values.empty());
rewriter.create<mlir::emitpybytecode::DictAdd>(
op.getLoc(), *result, key, value);
mlir::emitpybytecode::DictAdd::create(
rewriter, op.getLoc(), *result, key, value);
}
}
}
Expand DownExpand Up@@ -89,12 +89,12 @@ namespace {
llvm::ArrayRef<bool> requires_expansion)
{
auto list =
rewriter.create<mlir::emitpybytecode::BuildList>(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<mlir::emitpybytecode::ListExtend>(loc, list, el);
mlir::emitpybytecode::ListExtend::create(rewriter, loc, list, el);
} else {
rewriter.create<mlir::emitpybytecode::ListAppend>(loc, list, el);
mlir::emitpybytecode::ListAppend::create(rewriter, loc, list, el);
}
}
return list;
Expand DownExpand Up@@ -181,23 +181,23 @@ namespace {
for (auto [el, expand] : llvm::zip(op.getElements(), requires_expansion)) {
if (expand) {
if (!set.has_value()) {
set = rewriter.create<mlir::emitpybytecode::BuildSet>(
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<mlir::emitpybytecode::SetAdd>(
op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(
rewriter, op.getLoc(), *set, el);
}
}
elements.clear();
rewriter.create<mlir::emitpybytecode::SetUpdate>(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<mlir::emitpybytecode::SetAdd>(op.getLoc(), *set, el);
mlir::emitpybytecode::SetAdd::create(rewriter, op.getLoc(), *set, el);
}
rewriter.replaceOp(op, *set);
} else {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -97,8 +97,8 @@ namespace {
auto insertion_point = rewriter.getInsertionPoint();
auto *return_block = rewriter.createBlock(&op.getRegion());
auto value =
rewriter.create<mlir::py::ConstantOp>(op.getLoc(), rewriter.getNoneType());
rewriter.create<mlir::func::ReturnOp>(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;
})
Expand DownExpand Up@@ -136,9 +136,10 @@ namespace {
mlir::LogicalResult matchAndRewrite(mlir::py::YieldFromOp op,
mlir::PatternRewriter &rewriter) const final
{
auto iterator = rewriter.create<mlir::emitpybytecode::YieldFromIter>(
op.getLoc(), op.getIterable().getType(), op.getIterable());
auto value = rewriter.create<mlir::py::ConstantOp>(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<mlir::emitpybytecode::YieldFrom>(
op, iterator.getType(), iterator, value);
Expand Down
Loading
Loading