Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
aneeshdurg committed Sep 7, 2024
1 parent b620f3d commit 406ed7e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/test.yml
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: |
LLVM_CONFIG=llvm-config-18 cargo build
- name: "run tests"
run: |
cd test
LLVM_CONFIG=llvm-config-18 PYLLVM_PLUGIN_PATH=../target/debug/libpyllvmpass.so pytest
17 changes: 17 additions & 0 deletions test/replace_return.py
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
30 changes: 30 additions & 0 deletions test/test_module_pass.py
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()

0 comments on commit 406ed7e

Please sign in to comment.