Skip to content

cast<t>(immediate) should be make_const(t, immediate)#9061

Open
abadams wants to merge 1 commit intomainfrom
abadams/use_make_const
Open

cast<t>(immediate) should be make_const(t, immediate)#9061
abadams wants to merge 1 commit intomainfrom
abadams/use_make_const

Conversation

@abadams
Copy link
Member

@abadams abadams commented Mar 17, 2026

Doing a Halide cast of a c++ int constructs an immediate Expr (e.g. IntImm) and then eagerly folds it to a different type of immediate in the Cast constructor. It's better to just construct the immediate using the desired type to begin with.

Doing a Halide cast of a c++ int constructs an immediate Expr (e.g.
IntImm) and then eagerly folds it to a different type of immediate in
the Cast constructor. It's better to just construct the immediate using
the desired type to begin with.

Co-authored-by: Claude <noreply@anthropic.com>
@mcourteaux
Copy link
Contributor

This would be a good linting rule somehow, if we could define our own.

@abadams
Copy link
Member Author

abadams commented Mar 17, 2026

I found all the cases by adding things like this in IROperator.h:

template<typename T>
Expr cast(int64_t) {
  static_assert(false, "no!");
  return Expr{};
}

but I don't want to leave that because I don't want to break existing code outside the repo over a very very minor inefficiency.

@alexreinking
Copy link
Member

Failure is:

Seed: 6251522420976499283
can't prove upper bound: (1 <= 0)
a = 115
b = 10
c = 123
d = 85
e = 85
int32x2(x2(uint1(int32(uint1(c) && (uint1)1))) && uint1x2(ramp(b, 22, 2) % int32x2(ramp((uint32)75, (uint32)4294967192, 2))))
[0, 0]
In vector lane 1:
int32(uint1(int32(uint1(c) && (uint1)1)) && uint1((b + 22) % int32((uint32)4294967267))) -> 1
scope {
	a : [109, 115]
	b : [7, 117]
	c : [120, 123]
	d : [77, 109]
	e : [33, 119]
}

@mcourteaux
Copy link
Contributor

I found all the cases by adding things like this in IROperator.h:

template<typename T>
Expr cast(int64_t) {
  static_assert(false, "no!");
  return Expr{};
}

but I don't want to leave that because I don't want to break existing code outside the repo over a very very minor inefficiency.

Super clever! I wonder if there is a preprocessor define that we can use to check if we're building on CI, or if we're building just libHalide.so, @alexreinking?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants