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

feat[next][dace]: GTIR-to-SDFG lowering of cast_ builtin #1610

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@
}


def builtin_cast(*args: Any) -> str:
val, target_type = args
return MATH_BUILTINS_MAPPING[target_type].format(val)


def builtin_if(*args: Any) -> str:
cond, true_val, false_val = args
return f"{true_val} if {cond} else {false_val}"


GENERAL_BUILTIN_MAPPING: dict[str, Callable[[Any], str]] = {
"cast_": builtin_cast,
"if_": builtin_if,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,5 +795,8 @@ def visit_Literal(self, node: gtir.Literal) -> SymbolExpr:

def visit_SymRef(self, node: gtir.SymRef) -> IteratorExpr | MemletExpr | SymbolExpr:
param = str(node.id)
assert param in self.symbol_map
return self.symbol_map[param]
if param in self.symbol_map:
return self.symbol_map[param]
# if not in the lambda symbol map, this must be a symref to a builtin function
assert param in gtir_python_codegen.MATH_BUILTINS_MAPPING
return SymbolExpr(param, dace.string)
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,41 @@ def make_mesh_symbols(mesh: MeshDescriptor):
)


def test_gtir_copy():
def test_gtir_cast():
edopao marked this conversation as resolved.
Show resolved Hide resolved
domain = im.call("cartesian_domain")(
im.call("named_range")(gtir.AxisLiteral(value=IDim.value), 0, "size")
)
IFTYPE_INT64 = ts.FieldType(IFTYPE.dims, dtype=ts.ScalarType(kind=ts.ScalarKind.INT64))
testee = gtir.Program(
id="gtir_copy",
id="test_gtir_cast",
function_definitions=[],
params=[
gtir.Sym(id="x", type=IFTYPE),
gtir.Sym(id="x", type=IFTYPE_INT64),
gtir.Sym(id="y", type=IFTYPE),
gtir.Sym(id="size", type=SIZE_TYPE),
],
declarations=[],
body=[
gtir.SetAt(
expr=im.as_fieldop(im.lambda_("a")(im.deref("a")), domain)("x"),
expr=im.op_as_fieldop("divides", domain)(
im.as_fieldop(
im.lambda_("a")(im.call("cast_")(im.deref("a"), "float64")), domain
)("x"),
2.0,
),
domain=domain,
target=gtir.SymRef(id="y"),
)
],
)

a = np.random.rand(N)
b = np.empty_like(a)
a = np.ones(N, dtype=np.int64)
b = np.empty_like(a, dtype=np.float64)

sdfg = dace_backend.build_sdfg_from_gtir(testee, CARTESIAN_OFFSETS)

sdfg(x=a, y=b, **FSYMBOLS)
assert np.allclose(a, b)
assert np.allclose(b, 0.5)


def test_gtir_update():
Expand Down
Loading