Skip to content

Commit

Permalink
Provide the LLVM as a compiler backend (#73)
Browse files Browse the repository at this point in the history
* Also rename ArrayAllocate.type to element_type
  • Loading branch information
drhagen authored Aug 27, 2024
1 parent bc2013a commit e05eb8d
Show file tree
Hide file tree
Showing 29 changed files with 1,882 additions and 892 deletions.
2 changes: 1 addition & 1 deletion fuzz_tests/test_generate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from hypothesis import given

from tensora.compile import BroadcastTargetIndexError, TensorMethod
from tensora.desugar import DiagonalAccessError, NoKernelFoundError
from tensora.function import BroadcastTargetIndexError, TensorMethod

from .strategies import problem_and_tensors

Expand Down
9 changes: 8 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from nox import options, parametrize
from nox_poetry import Session, session

options.sessions = ["test", "test_taco", "test_numpy", "coverage", "lint"]
options.sessions = ["test", "test_taco", "test_cffi", "test_numpy", "coverage", "lint"]


@session(python=["3.10", "3.11", "3.12"])
Expand All @@ -18,6 +18,13 @@ def test_taco(s: Session):
s.run("python", "-m", "pytest", "--cov", "tensora", "tests/taco")


@session(python=["3.10", "3.11", "3.12"])
def test_cffi(s: Session):
s.install(".[cffi]", "pytest", "pytest-cov")
s.env["COVERAGE_FILE"] = f".coverage.cffi.{s.python}"
s.run("python", "-m", "pytest", "--cov", "tensora", "tests_cffi")


@session(python=["3.10", "3.11", "3.12"])
def test_numpy(s: Session):
s.install(".[numpy]", "pytest", "pytest-cov")
Expand Down
1,256 changes: 672 additions & 584 deletions poetry.lock

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ classifiers = [
python = "^3.10"
returns = "^0.20|^0.21|^0.22"
cffi = "^1.16"
# setuptools is required by cffi on Python 3.12 and up
# setuptools is required for cffi compilation on Python 3.12 and up
# https://cffi.readthedocs.io/en/latest/whatsnew.html#v1-16-0rc1
setuptools = { version = "^69", python = ">=3.12" }
setuptools = { version = "^69", python = ">=3.12", optional = true }
llvmlite = "^0.42"
parsita = "^2.0"
typer = "^0.9"
tensora-taco = { version = "^0.1.2", optional = true }
Expand Down Expand Up @@ -51,7 +52,8 @@ mkdocs-material = "^9"
tensora = "tensora.cli:app"

[tool.poetry.extras]
taco = ["tensora-taco"]
taco = ["tensora-taco", "setuptools"]
cffi = ["setuptools"]
numpy = ["numpy"]
scipy = ["scipy"]

Expand Down Expand Up @@ -98,7 +100,7 @@ extend-immutable-calls = ["hypothesis.strategies.integers"]

[tool.ruff.lint.per-file-ignores]
# Allow glob imports and uppercase identifiers in tests
"tests/*" = ["F403", "F405", "N802", "N806"]
"tests*/*" = ["F403", "F405", "N802", "N806"]

# F401: Allow unused imports in __init__.py
"__init__.py" = ["F401"]
Expand Down
2 changes: 1 addition & 1 deletion src/tensora/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .compile import evaluate, evaluate_taco, evaluate_tensora, tensor_method
from .format import Format, Mode
from .function import evaluate, evaluate_taco, evaluate_tensora, tensor_method
from .generate import TensorCompiler
from .tensor import Tensor
12 changes: 10 additions & 2 deletions src/tensora/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from .expression import parse_assignment
from .format import parse_named_format
from .generate import TensorCompiler, generate_c_code
from .generate import Language, TensorCompiler, generate_code
from .kernel_type import KernelType
from .problem import make_problem

Expand Down Expand Up @@ -52,6 +52,14 @@ def tensora(
help="The tensor algebra compiler to use to generate the kernel.",
),
] = TensorCompiler.tensora,
language: Annotated[
Language,
typer.Option(
"--language",
"-l",
help="The language in which to generate the kernel.",
),
] = Language.c,
output_path: Annotated[
Optional[Path],
typer.Option(
Expand Down Expand Up @@ -107,7 +115,7 @@ def tensora(
raise NotImplementedError()

# Generate code
match generate_c_code(problem, kernel_types, tensor_compiler):
match generate_code(problem, kernel_types, tensor_compiler, language):
case Failure(error):
typer.echo(str(error), err=True)
raise typer.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions src/tensora/codegen/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from ._ir_to_c import ir_to_c, ir_to_c_function_definition, ir_to_c_statement
from ._ir_to_llvm import ir_to_llvm
4 changes: 2 additions & 2 deletions src/tensora/codegen/_ir_to_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ def ir_to_c_boolean_to_integer(self: BooleanToInteger) -> str:

@ir_to_c_expression.register(ArrayAllocate)
def ir_to_c_array_allocate(self: ArrayAllocate) -> str:
return f"malloc(sizeof({type_to_c(self.type)}) * {parens(self.n_elements, (Add, Subtract))})"
return f"malloc(sizeof({type_to_c(self.element_type)}) * {parens(self.n_elements, (Add, Subtract))})"


@ir_to_c_expression.register(ArrayReallocate)
def ir_to_c_array_reallocate(self: ArrayReallocate) -> str:
old = ir_to_c_expression(self.old)
return f"realloc({old}, sizeof({type_to_c(self.type)}) * {parens(self.n_elements, (Add, Subtract))})"
return f"realloc({old}, sizeof({type_to_c(self.element_type)}) * {parens(self.n_elements, (Add, Subtract))})"


def ir_to_c_declaration(self: Declaration) -> str:
Expand Down
Loading

0 comments on commit e05eb8d

Please sign in to comment.