# LLVM intrinsic problem

**URL:** <https://forum.modular.com/t/llvm-intrinsic-problem/1144>\
**Category:** Mojo\
**Tags:** debugging\
**Created:** [March 26, 2025, 9:39pm UTC](https://forum.modular.com/t/llvm-intrinsic-problem/1144 "2025-03-26T21:39:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eyeless77](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@eyeless77](https://forum.modular.com/u/eyeless77)\
**Post date:** [March 26, 2025, 9:39pm UTC](https://forum.modular.com/t/llvm-intrinsic-problem/1144/1 "2025-03-26T21:39:52Z")

</div>

Hi, when compiling my mojo projects I’m getting an error:  
 ![Screenshot from 2025-03-26 22-32-59](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/8b981f531722ff96ec428e4d931558b90aea6f67.png)

And the piece of code causing it:

```auto
    @always_inline
    fn pext(self, b: BitBoard) -> UInt16:
        alias intrin = "llvm.x86.bmi.pext.64"
        return UInt16(llvm_intrinsic[intrin, UInt64](self.bb, b.bb))

```

Do anyone knows what is wrong?  
My version of Mojo: mojo 25.3.0.dev2025032405

---

<div class="post-metadata">

**Author:** ![owenhilyard](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/owenhilyard/32/41_2.png) [@owenhilyard](https://forum.modular.com/u/owenhilyard)\
**Post date:** [March 27, 2025, 1:02am UTC](https://forum.modular.com/t/llvm-intrinsic-problem/1144/2 "2025-03-27T01:02:15Z")

</div>

There’s somewhere that you’re running that at compile time, which LLVM doesn’t know how to do because that’s an x86 specific instruction. You can use [is\_compile\_time](https://docs.modular.com/mojo/stdlib/sys/compile/is_compile_time/) to add a fallback path.

---

<div class="post-metadata">

**Author:** ![eyeless77](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@eyeless77](https://forum.modular.com/u/eyeless77)\
**Post date:** [March 27, 2025, 10:02am UTC](https://forum.modular.com/t/llvm-intrinsic-problem/1144/3 "2025-03-27T10:02:26Z")

</div>

If I mark that I want to compile for a specific CPU that has this instruction available shouldn’t that fix a problem?

---

<div class="post-metadata">

**Author:** ![sora](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sora](https://forum.modular.com/u/sora)\
**Post date:** [March 27, 2025, 7:21pm UTC](https://forum.modular.com/t/llvm-intrinsic-problem/1144/4 "2025-03-27T19:21:43Z")

</div>

You could use `@parameter if` and queue the feature flags using `sys.info`:

> <https://github.com/modular/max/blob/20b297fb2ce43de2cac43aeabeeb0d5560221466/mojo/stdlib/src/builtin/simd.mojo#L851-L857>

Though the compile-time interpreter has limited capabilities:

> <https://github.com/modular/max/issues/933>
>
> \### Bug description
> 
> \`math.bit\` functions doesn't run at compile time.
> 
> \### …Steps to reproduce
> 
> Consider the following code:
> 
> \`\`\`mojo
> import math.bit as bit
> 
> fn main():
> alias n = bit.ctlz(10)
> 
> \`\`\`
> 
> It produces the following error message:
> 
> \`\`\`
> /\_\_w/modular/modular/Kernels/mojo/builtin/\_startup.mojo:70:1: error: no viable expansions found
> /\_\_w/modular/modular/Kernels/mojo/builtin/\_startup.mojo:70:1: note: call expansion failed - no concrete specializations
> /\_\_w/modular/modular/Kernels/mojo/builtin/\_startup.mojo:51:1: note: no viable expansions found
> /\_\_w/modular/modular/Kernels/mojo/builtin/\_startup.mojo:65:57: note: call expansion failed - no concrete specializations
> /\_\_w/modular/modular/Kernels/mojo/builtin/\_startup.mojo:29:1: note: no viable expansions found
> /\_\_w/modular/modular/Kernels/mojo/builtin/\_startup.mojo:42:18: note: call expansion failed - no concrete specializations
> /\_\_w/modular/modular/Kernels/mojo/builtin/\_startup.mojo:62:5: note: no viable expansions found
> /\_\_w/modular/modular/Kernels/mojo/builtin/\_startup.mojo:63:22: note: call expansion failed - no concrete specializations
> /workspaces/ubuntu/hello.mojo:3:1: note: no viable expansions found
> def main():
> ^
> /workspaces/ubuntu/hello.mojo:4:3: note: failed to evaluate 'apply'
> alias n = bit.ctlz(10)
> ^
> /\_\_w/modular/modular/Kernels/mojo/math/bit.mojo:81:1: note: failed to interpret function @$math::$bit::ctlz($builtin::$int::Int)\_concrete
> /\_\_w/modular/modular/Kernels/mojo/math/bit.mojo:90:44: note: failed to fold operation pop.call\_llvm\_intrinsic(10 : index, #pop\<simd false\> : !pop.scalar\<bool\>)
> mojo: error: failed to run the pass manager
> \`\`\`
> 
> \### System information
> 
> \`\`\`shell
> \- OS: Docker on Intel macOS
> \- mojo -v: mojo 0.3.0 (f64f9601)
> \- modular -v: modular 0.1.4 (6b54d308)
> \`\`\`

---

<div class="post-metadata">

**Author:** ![clattner](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/clattner/32/201_2.png) [@clattner](https://forum.modular.com/u/clattner)\
**Post date:** [March 30, 2025, 4:59pm UTC](https://forum.modular.com/t/llvm-intrinsic-problem/1144/5 "2025-03-30T16:59:38Z")

</div>

I think the comptime interpreter should be able to fold roughly ~~any intrinsic that LLVM does. This doesn’t include target-specific things like `llvm.x86.bmi.pext.64` (I agree is\_comp\_time is a good way to address that) but I’m seeing comments that it doesn’t support floor or bit intrinsics etc. Is that still true?

---

<div class="post-metadata">

**Author:** ![owenhilyard](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/owenhilyard/32/41_2.png) [@owenhilyard](https://forum.modular.com/u/owenhilyard)\
**Post date:** [March 30, 2025, 11:03pm UTC](https://forum.modular.com/t/llvm-intrinsic-problem/1144/6 "2025-03-30T23:03:48Z")

</div>

I think determining what can run at compile time is difficult for those of us outside of Modular don’t even have a good way to get the list of intrinsics without reading llvm source code. I’d ideally like to hook TableGen up to something that can spit out a few Mojo files so that we can better expose intrinsics without guesswork, but I think that might have to be part of the compiler build process. I think that most people also reach for the target specific options more readily, because they’re looking for “llvm pext intrinsic”, not the portable version, so they don’t see that the portable versions do work. We might want to guide people towards the portable options in order to avoid having lots of x86 specific code, since we should be able to polyfill a lot of things to have slower but at least functional code.

---

<div class="post-metadata">

**Author:** ![sora](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sora](https://forum.modular.com/u/sora)\
**Post date:** [March 31, 2025, 8:43pm UTC](https://forum.modular.com/t/llvm-intrinsic-problem/1144/7 "2025-03-31T20:43:57Z")

</div>

Simply answer, yes, that’s still true.

All related issues (That I know off):

- [[mojo-compiler] CompTime interpreter should be able to fold `pop.call_llvm_intrinsic` · Issue #933 · modular/max · GitHub](https://github.com/modular/max/issues/933)
- [[BUG] Cannot fold ` __floordiv__ ` for `Int` · Issue #3258 · modular/max · GitHub](https://github.com/modular/max/issues/3258)
- [[BUG] `0/0 != 1` returns `False` · Issue #3321 · modular/max · GitHub](https://github.com/modular/max/issues/3321)
- [[BUG] Fail to fold `index.shl` when the shift is large · Issue #3613 · modular/max · GitHub](https://github.com/modular/max/issues/3613)
- [[BUG] Count leading zeros can't be used at compile time · Issue #3847 · modular/max · GitHub](https://github.com/modular/max/issues/3847)
- [[Feature Request] [stdlib] Make all SIMD functions be able to be run at compile time · Issue #4142 · modular/max · GitHub](https://github.com/modular/max/issues/4142)
- [[stdlib] Add compile and runtime utf8 `debug_assert` validation for `StringSlice` by martinvuyk · Pull Request #4134 · modular/max · GitHub](https://github.com/modular/max/pull/4134)

---

<div class="post-metadata">

**Author:** ![clattner](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/clattner/32/201_2.png) [@clattner](https://forum.modular.com/u/clattner)\
**Post date:** [April 4, 2025, 5:02am UTC](https://forum.modular.com/t/llvm-intrinsic-problem/1144/8 "2025-04-04T05:02:21Z")

</div>

It turns out that there were two separate problems that have both been fixed: we weren’t able to fold operations that returned integer simd results (like ctlz on SIMD) , and we weren’t able to fold index dialect operations (affecting Int and UInt). These have both been fixed now, it would be wonderful if you could check to see if all of those issues are fixed and if not, plz let me know.

Thank you @sora!
