-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b620f3d
commit 7c18024
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
test: | ||
name: Test PyLLVMPass | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: "checkout repository" | ||
uses: actions/checkout@v4 | ||
|
||
- name: "install rust" | ||
uses: actions-rust-lang/setup-rust-toolchain@v1 | ||
|
||
- name: "install llvm-18" | ||
run: | | ||
wget https://apt.llvm.org/llvm.sh | ||
chmod u+x llvm.sh | ||
sudo ./llvm.sh 18 | ||
- name: "install pytest" | ||
run: | | ||
pip install pytest | ||
- name: "build" | ||
run: | | ||
cargo build | ||
- name: "run tests" | ||
run: | | ||
cd test | ||
LLVM_CONFIG=llvm-config-18 PYLLVM_PLUGIN_PATH=../target/debug/libpyllvmpass.so pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""LLVM Module pass that replace all return statements with `return 1`""" | ||
|
||
import llvmcpy.llvm as cllvm | ||
|
||
def run_on_module(module: cllvm.Module): | ||
ctx = module.get_context() | ||
i32_t = ctx.int32_type() | ||
# constant i32(1) | ||
const_1 = i32_t.const_int(1, False) | ||
|
||
for fn in module.iter_functions(): | ||
for bb in fn.iter_basic_blocks(): | ||
for inst in bb.iter_instructions(): | ||
if inst.instruction_opcode != cllvm.Ret: | ||
continue | ||
inst.set_arg_operand(0, const_1) | ||
return 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import os | ||
import subprocess | ||
import pytest | ||
|
||
@pytest.fixture | ||
def plugin_path(): | ||
return os.environ["PYLLVM_PLUGIN_PATH"] | ||
|
||
|
||
def test_replace_return(tmp_path, plugin_path): | ||
src = tmp_path / 'main.cpp' | ||
with src.open('w') as f: | ||
f.write("int main() { return 0; }\n") | ||
ll = tmp_path/'main.ll' | ||
subprocess.check_call(['clang-18', '-O3', src, '-S', '-emit-llvm', '-o', ll]) | ||
assert 'ret i32 0' in ll.read_text() | ||
|
||
outfile = tmp_path/'out.ll' | ||
subprocess.check_call([ | ||
'opt-18', | ||
'--load-pass-plugin', | ||
plugin_path, | ||
'--passes=pyllvmpass[replace_return]', | ||
ll, | ||
'-S', | ||
'-o', | ||
outfile | ||
]) | ||
assert 'ret i32 0' not in outfile.read_text() | ||
assert 'ret i32 1' in outfile.read_text() |