Skip to content

Commit 778aac9

Browse files
committed
add initial cost-model bindings
1 parent 1a98ba4 commit 778aac9

15 files changed

Lines changed: 487 additions & 79 deletions

File tree

.github/workflows/build-cinnamon.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ checkout_and_build_llvm="${checkout_and_build_llvm:-0}"
1212
checkout_and_build_torch_mlir="${checkout_and_build_torch_mlir:-0}"
1313
checkout_upmem="${checkout_upmem:-0}"
1414
CINNAMON_CMAKE_OPTIONS="${CINNAMON_CMAKE_OPTIONS:-}"
15+
CINNAMON_BUILD_OPTIONS="${CINNAMON_BUILD_OPTIONS:-}"
1516

1617
# Required paths (defined in common.sh)
1718
project_root="${project_root:?Define 'project_root' in common.sh}"
@@ -98,6 +99,8 @@ configure() {
9899
-G Ninja
99100
-DCMAKE_BUILD_TYPE=RelWithDebInfo
100101
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
102+
-DLLVM_ENABLE_EH=ON
103+
-DLLVM_ENABLE_RTTI=ON
101104
)
102105

103106
if ((${#DEP_OPTS[@]})); then
@@ -115,13 +118,12 @@ configure() {
115118

116119
# ---- Build with one clean retry on failure ----
117120
status "Building Cinnamon (Ninja)"
118-
cmake --build build --target all -j 4
119-
# if ! cmake --build build --target all -j 8; then
120-
# warning "Build failed — cleaning build/ and retrying from fresh configure…"
121-
# rm -rf build
122-
# configure
123-
# cmake --build build --target all -j 8
124-
# fi
121+
if ! cmake --build build --target all $CINNAMON_BUILD_OPTIONS; then
122+
warning "Build failed — cleaning build/ and retrying from fresh configure…"
123+
rm -rf build
124+
configure
125+
cmake --build build --target all $CINNAMON_BUILD_OPTIONS
126+
fi
125127

126128
# ---- Python package wiring (optional) ----
127129
if [[ "$setup_python_venv" -eq 1 && -n "${llvm_path:-}" && -n "${torch_mlir_path:-}" ]]; then

.github/workflows/build-llvm.sh

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -77,58 +77,49 @@ if [[ -n "$clean_reason" ]]; then
7777
rm -rf build
7878
mkdir -p build
7979
fi
80-
# ---- Always run configure (idempotent) ----
81-
status "Configuring LLVM (Ninja; always run to catch changes)"
82-
print_and_run cmake -S llvm -B build -G Ninja \
83-
-Wno-dev \
84-
-DLLVM_ENABLE_PROJECTS="$LLVM_PROJECTS" \
85-
-DLLVM_TARGETS_TO_BUILD="$LLVM_TARGETS_TO_BUILD" \
86-
-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="$LLVM_EXPERIMENTAL_TARGETS" \
87-
-DLLVM_ENABLE_ASSERTIONS=ON \
88-
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
89-
-DLLVM_BUILD_TOOLS=ON \
90-
-DCMAKE_BUILD_TYPE=Release \
91-
-DBUILD_SHARED_LIBS=ON \
92-
-DLLVM_INCLUDE_TESTS=OFF \
93-
-DLLVM_INCLUDE_BENCHMARKS=OFF \
94-
-DLLVM_OPTIMIZED_TABLEGEN=ON \
95-
-DLLVM_CCACHE_BUILD=ON \
96-
-DLLVM_PARALLEL_COMPILE_JOBS=4 \
97-
-DLLVM_PARALLEL_LINK_JOBS=1 \
98-
-DLLVM_PARALLEL_TABLEGEN_JOBS=4 \
99-
"${EXTRA_CMAKE_OPTS[@]}"
100-
101-
# Save config hash so we can detect future changes
102-
echo "$CURRENT_HASH" > "$HASH_FILE"
103-
104-
# Sanity: ensure build.ninja exists
105-
[[ -f build/build.ninja ]] || { error "CMake configure did not produce build/build.ninja."; exit 1; }
10680

107-
# ---- Build with one clean-retry ----
108-
status "Building LLVM (Ninja)"
109-
if ! cmake --build build --target ${LLVM_BUILD_TARGETS}; then
110-
warning "Build failed — cleaning build/ and retrying from fresh configure…"
111-
rm -rf build
112-
cmake -S llvm -B build -G Ninja \
81+
# ---- Configure helper ----
82+
configure() {
83+
status "Configuring LLVM (Ninja; always run to catch changes)"
84+
85+
print_and_run cmake -S llvm -B build -G Ninja \
11386
-Wno-dev \
114-
-DLLVM_ENABLE_PROJECTS="$LLVM_PROJECTS" \
115-
-DLLVM_TARGETS_TO_BUILD="$LLVM_TARGETS_TO_BUILD" \
116-
-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="$LLVM_EXPERIMENTAL_TARGETS" \
117-
-DLLVM_ENABLE_ASSERTIONS=ON \
118-
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
119-
-DLLVM_BUILD_TOOLS=ON \
12087
-DCMAKE_BUILD_TYPE=Release \
12188
-DBUILD_SHARED_LIBS=ON \
122-
-DLLVM_INCLUDE_TESTS=OFF \
89+
-DLLVM_BUILD_TOOLS=ON \
90+
-DLLVM_CCACHE_BUILD=ON \
91+
-DLLVM_ENABLE_PROJECTS="$LLVM_PROJECTS" \
92+
-DLLVM_ENABLE_ASSERTIONS=ON \
93+
-DLLVM_ENABLE_EH=ON \
94+
-DLLVM_ENABLE_RTTI=ON \
95+
-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="$LLVM_EXPERIMENTAL_TARGETS" \
12396
-DLLVM_INCLUDE_BENCHMARKS=OFF \
97+
-DLLVM_INCLUDE_TESTS=OFF \
12498
-DLLVM_OPTIMIZED_TABLEGEN=ON \
125-
-DLLVM_CCACHE_BUILD=ON \
126-
-DLLVM_PARALLEL_COMPILE_JOBS=4 \
127-
-DLLVM_PARALLEL_LINK_JOBS=1 \
128-
-DLLVM_PARALLEL_TABLEGEN_JOBS=4 \
99+
-DMLIR_ENABLE_BINDINGS_PYTHON=ON \
100+
-DLLVM_TARGETS_TO_BUILD="$LLVM_TARGETS_TO_BUILD" \
129101
"${EXTRA_CMAKE_OPTS[@]}"
130-
cmake --build build --target ${LLVM_BUILD_TARGETS}
131-
fi
102+
103+
# Save config hash so we can detect future changes
104+
echo "$CURRENT_HASH" > "$HASH_FILE"
105+
106+
# Sanity: ensure build.ninja exists
107+
[[ -f build/build.ninja ]] || { error "CMake configure did not produce build/build.ninja."; exit 1; }
108+
}
109+
110+
# ---- Always run configure (idempotent) ----
111+
configure
112+
113+
# ---- Build with one clean-retry ----
114+
status "Building LLVM (Ninja)"
115+
cmake --build build --target ${LLVM_BUILD_TARGETS}
116+
117+
#if ! cmake --build build --target ${LLVM_BUILD_TARGETS}; then
118+
# warning "Build failed — cleaning build/ and retrying from fresh configure…"
119+
# rm -rf build
120+
# configure
121+
# cmake --build build --target ${LLVM_BUILD_TARGETS}
122+
#fi
132123

133124
export PATH="$llvm_path/build/bin:$PATH"
134125
popd >/dev/null

.github/workflows/build-torch.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ if [[ $checkout_and_build_torch_mlir -eq 1 ]]; then
7676
$dependency_paths \
7777
-Wno-dev \
7878
-DCMAKE_BUILD_TYPE=Release \
79+
-DLLVM_ENABLE_EH=ON \
80+
-DLLVM_ENABLE_RTTI=ON \
7981
-DTORCH_MLIR_OUT_OF_TREE_BUILD=ON \
8082
-DTORCH_MLIR_ENABLE_STABLEHLO=OFF \
8183
-U CMAKE_EXE_LINKER_FLAGS -U CMAKE_SHARED_LINKER_FLAGS \

.github/workflows/common.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#!/bin/bash
22

3+
# Load .env file and export all variables
4+
if [ -f ".env" ]; then
5+
set -o allexport
6+
source .env
7+
set +o allexport
8+
fi
9+
310
if [[ -z "${PREAMBLE_LOADED:-}" ]]; then
411
PREAMBLE_LOADED="1"
512

.gitignore

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
.cache
2-
/.vscode/
3-
/.idea/
42
.directory
5-
/.venv/
6-
/third-party/ALPINE/
7-
/third-party/llvm
8-
/third-party/torch-mlir/
9-
/third-party/upmem*/
10-
/.env
11-
/testbench/gen
3+
.env
4+
.idea/
5+
.venv/
6+
.vscode/
7+
third-party
8+
testbench/gen
129

1310

1411
# Created by https://www.toptal.com/developers/gitignore/api/cmake
@@ -40,5 +37,3 @@ sandbox
4037
python/cinnamon/_resources
4138
.env
4239
**/__pycache__
43-
44-

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "third-party/pybind11"]
2+
path = third-party/pybind11
3+
url = https://github.com/pybind/pybind11
4+
branch = stable

CMakeLists.txt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (TORCH_MLIR_DIR)
3434
get_property(CINM_DEPENDENCY_LIBS GLOBAL PROPERTY CINM_DEPENDENCY_LIBS)
3535
list(APPEND CINM_DEPENDENCY_LIBS ${TORCH_MLIR_LIBS})
3636
set_property(GLOBAL PROPERTY CINM_DEPENDENCY_LIBS ${CINM_DEPENDENCY_LIBS})
37-
37+
3838
else()
3939
message(WARNING "TORCH_MLIR_DIR not set, torch frontend wont be available.")
4040
endif()
@@ -111,11 +111,11 @@ if (TORCH_MLIR_DIR)
111111
add_library(Torch::TorchMLIRTorchConversionDialect STATIC IMPORTED GLOBAL)
112112
set_property(TARGET Torch::TorchMLIRTorchConversionDialect PROPERTY
113113
IMPORTED_LOCATION ${TORCH_MLIR_DIR}/lib/libTorchMLIRTorchConversionDialect.a)
114-
114+
115115
add_library(Torch::All INTERFACE IMPORTED)
116116
set_property(TARGET Torch::All PROPERTY
117-
INTERFACE_LINK_LIBRARIES Torch::TorchMLIRTorchDialect
118-
Torch::TorchMLIRTorchUtils
117+
INTERFACE_LINK_LIBRARIES Torch::TorchMLIRTorchDialect
118+
Torch::TorchMLIRTorchUtils
119119
Torch::TorchMLIRTorchConversionDialect)
120120
target_include_directories(Torch::All INTERFACE ${TORCH_MLIR_INCLUDE_DIR})
121121

@@ -135,6 +135,20 @@ else()
135135
endif()
136136

137137

138+
# Add pybind11
139+
find_package(pybind11 OPTIONAL)
140+
if (NOT pybind11_FOUND)
141+
set(PYBIND11_FINDPYTHON ON)
142+
add_subdirectory("third-party/pybind11")
143+
message(STATUS "using third-party/pybind11")
144+
endif()
145+
146+
execute_process(COMMAND python3 -m pybind11 --includes OUTPUT_VARIABLE PYBIND_INCLUDES OUTPUT_STRIP_TRAILING_WHITESPACE)
147+
message(STATUS "using python headers from ${PYBIND_INCLUDES}")
148+
separate_arguments(PYBIND_INCLUDES UNIX_COMMAND "${PYBIND_INCLUDES}")
149+
add_compile_options(${PYBIND_INCLUDES})
150+
151+
138152
add_subdirectory(include)
139153
add_subdirectory(lib)
140154
add_subdirectory(test)

cost_model_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random
2+
3+
from xdsl.context import Context
4+
from xdsl.parser import Parser
5+
6+
name = "cost_model_test"
7+
passes = "cinm-tiling"
8+
operations = {
9+
"cinm.compute"
10+
}
11+
12+
ctx = Context(allow_unregistered=True)
13+
14+
# def run(op: str, elementType: str, operand_dimensions: list[list[int]], location: str) -> float :
15+
# print(op, elementType, operand_dimensions, location)
16+
# return random.uniform(0.0, 10.0)
17+
18+
def run(ir: str, location: str) -> float :
19+
parser = Parser(ctx, ir)
20+
compute_op = parser.parse_operation()
21+
print(ir)
22+
23+
print(parser.forward_ssa_references)
24+
25+
for op in compute_op.walk():
26+
op.name = op.get_attr_or_prop("op_name__").data
27+
28+
cinm_ops = [op for op in compute_op.walk()]
29+
return len(cinm_ops) + random.uniform(0.0, 10.0)

cost_model_test2.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import random
2+
3+
from xdsl.context import Context
4+
from xdsl.parser import Parser
5+
6+
name = "cost_model_test_2"
7+
passes = ""
8+
operations = {
9+
"cinm.compute"
10+
}
11+
12+
ctx = Context(allow_unregistered=True)
13+
14+
# def run(op: str, elementType: str, operand_dimensions: list[list[int]], location: str) -> float :
15+
# print(op, elementType, operand_dimensions, location)
16+
# return random.uniform(0.0, 10.0)
17+
18+
def run(ir: str, location: str) -> float :
19+
parser = Parser(ctx, ir)
20+
compute_op = parser.parse_operation()
21+
print(ir)
22+
23+
print(parser.forward_ssa_references)
24+
25+
for op in compute_op.walk():
26+
op.name = op.get_attr_or_prop("op_name__").data
27+
28+
cinm_ops = [op for op in compute_op.walk()]
29+
return len(cinm_ops) + random.uniform(0.0, 10.0)

include/cinm-mlir/Dialect/Cinm/IR/CinmAttributes.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,19 @@ def Cinm_RoundingMode : I32EnumAttr<
104104
let cppNamespace = "::mlir::cinm";
105105
}
106106

107+
//===----------------------------------------------------------------------===//
108+
// Cost-Model
109+
//===----------------------------------------------------------------------===//
110+
111+
def Cinm_CostModelData : Cinm_Attr<"CostModelData"> {
112+
let parameters = (ins
113+
"StringAttr": $name,
114+
"FloatAttr": $cost
115+
);
116+
117+
let mnemonic = "cost_model_data";
118+
let assemblyFormat = "`<` struct($name, $cost) `>`";
119+
}
120+
121+
107122
#endif

0 commit comments

Comments
 (0)