Skip to content

Commit 7a94ad8

Browse files
authored
Lowering Through LLVM (#26)
* add poly_to_llvm test file * add starter poly-to-llvm pipeline * add convert-func-to-llvm * add arith-to-llvm * add elementwise-to-linalg * add back arith-to-llvm * add tensor-to-linalg * add do-nothing linalg-to-loops pass * add scf-to-cf and cf-to-llvm passes * update upstream MLIR commit * fix conj pattern broken by mlir update * migrate use of constFoldBinaryOp * fix conflict caused by new function overload * move arith-to-llvm to the end of the pass * add one-shot bufferization pass * move func-to-llvm after bufferization, enabling linalg-to-loops * memref-expand-strided-metadata and finalize-memref-to-llvm * move func-to-llvm even later * add canonicalization cleanup * encode end-to-end compilation pipeline as a test * add a poly.eval-specific end-to-end test * ensure llc uses the same PositionIndependentExecutable option as clang * fix the off-by-one error in the lowering of eval * udpate cmake build to use same LLVM commit * update subproject commit * add project_src_dir so tests work with both bazel and cmake --------- Co-authored-by: Jeremy Kun <j2kun@users.noreply.github.com>
1 parent 3dc2169 commit 7a94ad8

File tree

17 files changed

+132
-17
lines changed

17 files changed

+132
-17
lines changed

.github/workflows/build_and_test_cmake.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
with:
2828
path: |
2929
./externals/llvm-project
30-
key: ${{ runner.os }}-norm-${{ hashFiles('**/CMakeLists.txt') }}
30+
key: ${{ runner.os }}-cmake-${{ hashFiles('bazel/import_llvm.bzl') }}-${{ hashFiles('**/CMakeLists.txt') }}
3131

3232
- name: Git config
3333
run: |
@@ -36,8 +36,10 @@ jobs:
3636
- name: Build LLVM
3737
if: steps.cache-llvm.outputs.cache-hit != 'true'
3838
run: |
39+
LLVM_COMMIT=$(grep LLVM_COMMIT ${GITHUB_WORKSPACE}/bazel/import_llvm.bzl | head -n 1 | cut -d'"' -f 2 )
3940
git submodule update --init --recursive
4041
cd externals/llvm-project
42+
git checkout ${LLVM_COMMIT}
4143
mkdir build && cd build
4244
cmake -G Ninja ../llvm -DLLVM_ENABLE_PROJECTS=mlir -DLLVM_BUILD_EXAMPLES=ON -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=ON -DLLVM_TARGETS_TO_BUILD="host"
4345
cmake --build . --target check-mlir
@@ -50,4 +52,4 @@ jobs:
5052
cmake --build . --target MLIRMulToAddPasses
5153
cmake --build . --target mlir-headers
5254
cmake --build . --target tutorial-opt
53-
cmake --build . --target check-mlir-tutorial
55+
cmake --build . --target check-mlir-tutorial

bazel/import_llvm.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ load(
88
def import_llvm(name):
99
"""Imports LLVM."""
1010

11-
# June 5, 2023
12-
LLVM_COMMIT = "cd5fcea6d4c70a7328ca9538c9098d9f5af69682"
11+
# 2023-10-30
12+
LLVM_COMMIT = "896749aa0d420ae573255a64a349bc2a76cfed37"
1313

1414
new_git_repository(
1515
name = name,

externals/llvm-project

lib/Conversion/PolyToStandard/PolyToStandard.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct ConvertAdd : public OpConversionPattern<AddOp> {
4848
ConversionPatternRewriter &rewriter) const override {
4949
arith::AddIOp addOp = rewriter.create<arith::AddIOp>(
5050
op.getLoc(), adaptor.getLhs(), adaptor.getRhs());
51-
rewriter.replaceOp(op.getOperation(), {addOp});
51+
rewriter.replaceOp(op.getOperation(), addOp);
5252
return success();
5353
}
5454
};
@@ -64,7 +64,7 @@ struct ConvertSub : public OpConversionPattern<SubOp> {
6464
ConversionPatternRewriter &rewriter) const override {
6565
arith::SubIOp subOp = rewriter.create<arith::SubIOp>(
6666
op.getLoc(), adaptor.getLhs(), adaptor.getRhs());
67-
rewriter.replaceOp(op.getOperation(), {subOp});
67+
rewriter.replaceOp(op.getOperation(), subOp);
6868
return success();
6969
}
7070
};
@@ -149,6 +149,8 @@ struct ConvertEval : public OpConversionPattern<EvalOp> {
149149
auto lowerBound =
150150
b.create<arith::ConstantOp>(b.getIndexType(), b.getIndexAttr(1));
151151
auto numTermsOp = b.create<arith::ConstantOp>(b.getIndexType(),
152+
b.getIndexAttr(numTerms));
153+
auto upperBound = b.create<arith::ConstantOp>(b.getIndexType(),
152154
b.getIndexAttr(numTerms + 1));
153155
auto step = lowerBound;
154156

@@ -163,7 +165,7 @@ struct ConvertEval : public OpConversionPattern<EvalOp> {
163165
auto accum =
164166
b.create<arith::ConstantOp>(b.getI32Type(), b.getI32IntegerAttr(0));
165167
auto loop = b.create<scf::ForOp>(
166-
lowerBound, numTermsOp, step, accum.getResult(),
168+
lowerBound, upperBound, step, accum.getResult(),
167169
[&](OpBuilder &builder, Location loc, Value loopIndex,
168170
ValueRange loopState) {
169171
ImplicitLocOpBuilder b(op.getLoc(), builder);

lib/Dialect/Poly/PolyOps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ OpFoldResult ConstantOp::fold(ConstantOp::FoldAdaptor adaptor) {
1616
}
1717

1818
OpFoldResult AddOp::fold(AddOp::FoldAdaptor adaptor) {
19-
return constFoldBinaryOp<IntegerAttr, APInt>(
19+
return constFoldBinaryOp<IntegerAttr, APInt, void>(
2020
adaptor.getOperands(), [&](APInt a, APInt b) { return a + b; });
2121
}
2222

2323
OpFoldResult SubOp::fold(SubOp::FoldAdaptor adaptor) {
24-
return constFoldBinaryOp<IntegerAttr, APInt>(
24+
return constFoldBinaryOp<IntegerAttr, APInt, void>(
2525
adaptor.getOperands(), [&](APInt a, APInt b) { return a - b; });
2626
}
2727

lib/Dialect/Poly/PolyPatterns.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ include "mlir/Dialect/Complex/IR/ComplexOps.td"
66
include "mlir/IR/PatternBase.td"
77

88
def LiftConjThroughEval : Pat<
9-
(Poly_EvalOp $f, (ConjOp $z)),
10-
(ConjOp (Poly_EvalOp $f, $z))
9+
(Poly_EvalOp $f, (ConjOp $z, $fastmath)),
10+
(ConjOp (Poly_EvalOp $f, $z), $fastmath)
1111
>;
1212

1313
def HasOneUse: Constraint<CPred<"$_self.hasOneUse()">, "has one use">;

lib/Transform/Arith/MulToAdd.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct PowerOfTwoExpand : public OpRewritePattern<MulIOp> {
4646
MulIOp newMul = rewriter.create<MulIOp>(op.getLoc(), lhs, newConstant);
4747
AddIOp newAdd = rewriter.create<AddIOp>(op.getLoc(), newMul, newMul);
4848

49-
rewriter.replaceOp(op, {newAdd});
49+
rewriter.replaceOp(op, newAdd);
5050
rewriter.eraseOp(rhsDefiningOp);
5151

5252
return success();
@@ -79,7 +79,7 @@ struct PeelFromMul : public OpRewritePattern<MulIOp> {
7979
MulIOp newMul = rewriter.create<MulIOp>(op.getLoc(), lhs, newConstant);
8080
AddIOp newAdd = rewriter.create<AddIOp>(op.getLoc(), newMul, lhs);
8181

82-
rewriter.replaceOp(op, {newAdd});
82+
rewriter.replaceOp(op, newAdd);
8383
rewriter.eraseOp(rhsDefiningOp);
8484

8585
return success();

tests/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ filegroup(
66
testonly = True,
77
data = [
88
"//tests:lit.cfg.py",
9+
"//tests:poly_to_llvm_main.c",
910
"//tools:tutorial-opt",
11+
"@llvm-project//clang:clang",
1012
"@llvm-project//llvm:FileCheck",
1113
"@llvm-project//llvm:count",
14+
"@llvm-project//llvm:llc",
1215
"@llvm-project//llvm:not",
1316
"@llvm-project//mlir:mlir-cpu-runner",
1417
"@llvm-project//mlir:mlir-opt",
18+
"@llvm-project//mlir:mlir-translate",
1519
"@mlir_tutorial_pip_deps_lit//:pkg",
1620
],
1721
)

tests/lit.cfg.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@
4040
+ ":"
4141
+ os.environ["PATH"]
4242
)
43+
44+
substitutions = {
45+
"%project_source_dir": runfiles_dir.joinpath(Path('mlir_tutorial')),
46+
}
47+
config.substitutions.extend(substitutions.items())

tests/lit.cmake.cfg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
config.substitutions.append(("%PATH%", config.environment["PATH"]))
2727
config.substitutions.append(("%shlibext", config.llvm_shlib_ext))
28+
config.substitutions.append(("%project_source_dir", config.project_source_dir))
2829

2930
llvm_config.with_system_environment(["HOME", "INCLUDE", "LIB", "TMP", "TEMP"])
3031

@@ -49,4 +50,4 @@
4950
"tutorial-opt"
5051
]
5152

52-
llvm_config.add_tool_substitutions(tools, tool_dirs)
53+
llvm_config.add_tool_substitutions(tools, tool_dirs)

0 commit comments

Comments
 (0)