Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test ksc-mlir on examples from test/ksc #663

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion mlir/run-test-examples.sh

This file was deleted.

69 changes: 0 additions & 69 deletions mlir/test/Ksc/examples.ks

This file was deleted.

72 changes: 72 additions & 0 deletions src/python/ksc/prune_and_compile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Invoke ksc on a file, treating any def which appears in the file as a root.
# (But any unused functions in the prelude are pruned.)

import subprocess
import sys

from ksc import utils
from ksc.parse_ks import parse_ks_filename
from ksc.expr import Def, GDef
from ksc.type_propagate import type_propagate_decls


def function_name(decl):
if isinstance(decl, Def):
return str(decl.name)
# elif isinstance(decl, GDef):
# return str(decl.name())
else:
return None


def prune_and_compile(source_file, output_file):
ksc_path, ksc_runtime_dir = utils.get_ksc_paths()

symtab = dict()
decls_prelude = list(parse_ks_filename(f"{ksc_runtime_dir}/prelude.ks"))
type_propagate_decls(decls_prelude, symtab)
decls = list(parse_ks_filename(source_file))
type_propagate_decls(decls, symtab)

function_names = [
fn for fn in (function_name(decl) for decl in decls) if fn is not None
]

ksc_command = [
ksc_path,
"--generate-cpp",
"--ks-source-file",
f"{ksc_runtime_dir}/prelude.ks",
"--ks-source-file",
source_file,
"--ks-output-file",
output_file,
"--cpp-include",
"prelude.h",
"--cpp-output-file",
"/dev/null",
"--remove-unused",
*(opt for function_name in function_names for opt in ("--used", function_name)),
]

try:
e = subprocess.run(ksc_command, capture_output=True, check=True)
except subprocess.CalledProcessError as e:
ksc_command_str = " ".join(ksc_command)
print(f"Command failed:\n{ksc_command_str}")
print("KSC output:\n")
print(e.output.decode("ascii"))
ksc_stderr = e.stderr.decode("ascii")
ksc_stderr_filtered = "> " + "\n> ".join(ksc_stderr.split("\n")[:-1])
print(ksc_stderr_filtered + "\n")
raise Exception(
f"Command failed:\n"
f"{ksc_command_str}\n"
f"KSC output:\n"
f"{ksc_stderr_filtered}\n"
) from e


if __name__ == "__main__":
assert len(sys.argv) == 3
prune_and_compile(sys.argv[1], sys.argv[2])
22 changes: 20 additions & 2 deletions test/builds/build_and_test_mlir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ jobs:
vmImage: 'ubuntu-18.04'

steps:


- task: UsePythonVersion@0
inputs:
versionSpec: '3.8'
architecture: 'x64'

- script: sh ./test/builds/mkdirs.sh
displayName: 'Make directories [userInstall]'

- script: echo "##vso[task.setvariable variable=LLVM_BRANCH]$(cat $(Build.Repository.LocalPath)/etc/llvm-branch.txt)"

- task: Cache@2
Expand All @@ -27,4 +35,14 @@ jobs:
condition: ne(variables.CACHE_RESTORED, 'true')

- script: sh ./test/builds/build_and_test_kscmlir.sh
displayName: Testing Ksc MLIR [userTest]
displayName: Building and testing Ksc MLIR [userTest]

- script: sh ./test/builds/install_linux.sh || (sleep 30 && sh ./test/builds/install_linux.sh) || (sleep 30 && sh ./test/builds/install_linux.sh)
displayName: 'Install dependencies [userInstall]'

- script: /opt/cabal/3.0/bin/cabal v2-install --with-ghc /opt/ghc/8.6.5/bin/ghc-8.6.5 --installdir=build/bin --overwrite-policy=always --install-method=copy
displayName: 'Building ksc [userInstall]'

- script: sh ./test/builds/test_kscmlir_examples.sh .
displayName: Testing Ksc MLIR on examples from ksc [userTest]

15 changes: 15 additions & 0 deletions test/builds/test_kscmlir_examples.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set -e

echo Installing dependencies...
python3 -m pip install -r src/python/requirements.txt -f https://download.pytorch.org/whl/torch_stable.html

KNOSSOS=$1
KSCPY=$KNOSSOS/src/python

mkdir -p obj/test/kscmlir/examples

for example in ex0; do
echo "Testing example $example.ks"
PYTHONPATH=$KSCPY python3 src/python/ksc/prune_and_compile.py test/ksc/$example.ks obj/test/kscmlir/examples/$example.kso
./build/bin/ksc-mlir MLIR obj/test/kscmlir/examples/$example.kso > /dev/null
done