[Feature Request] [mojo-lang] A version of `is_compile_time()` that can run in a `comptime` expression
@dgurchenkov is already working on this.
Since Mar 30, 2026.
- Dominant language
- Mojo
- Stars
- 29.8k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
Review Mojo's priorities
- I have read the roadmap and priorities and I believe this request falls within the priorities.
What is your request?
With the previous version of is_compile_time that could only be called outside a comptime expression, this would fail:
from sys import is_compile_time
fn asd[param: Bool]() -> String:
if is_compile_time():
return "hi"
comptime assert param, "should never happen"
return "what"
fn main():
comptime res = asd[False]()
print(res)
What I would like is a way to incorporate that the code is going to be run at compile time inside the comptime if expression:
fn asd[param: Bool]() -> String:
comptime if is_compile_time():
return "hi"
comptime assert param, "should never happen"
return "what"
What is your motivation for this change?
As an example, currently one can't run the cosine function for float64 in an Nvidia GPU at compile time (only float32 is supported) even though the one running the code is the CPU and not the GPU. A simplified version of the code looks like this:
fn cos[dtype: DType](x: Scalar[dtype]) -> type_of(x):
comptime if size_of[dtype]() < size_of[DType.float32]():
return cos(x.cast[DType.float32]()).cast[dtype]()
if is_compile_time():
return _llvm_unary_fn["llvm.cos"](x)
comptime if is_nvidia_gpu() and dtype == DType.float32:
return _call_ptx_intrinsic[
instruction="cos.approx.ftz.f32", constraints="=f,f"
](x)
elif is_apple_gpu():
return _llvm_unary_fn["llvm.air.cos"](x)
else:
comptime assert (
not is_nvidia_gpu() or dtype != DType.float64
), "DType.float64 is not supported on NVIDIA GPU"
return _llvm_unary_fn["llvm.cos"](x)
if we could integrate the fact that it's running at compile time into the branches:
comptime if size_of[dtype]() < size_of[DType.float32]():
return cos(x.cast[DType.float32]()).cast[dtype]()
elif is_compile_time():
return _llvm_unary_fn["llvm.cos"](x)
elif is_nvidia_gpu() and dtype == DType.float32:
the issue would be solved
Any other details?
This is a rewording of the issue MOCO-3291 to be considered a feature request instead of a bug. We need to be able to run compile time code differently than the capabilities of the destination device; this will bring more issues as we incorporate more weird devices in time.
CC: @weiweichen @owenhilyard
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.