[llvm] [ConstantTime][LLVM] Add llvm.ct.select intrinsic with generic SelectionDAG lowering (PR #166702)
Akshay K via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 11 07:55:52 PST 2026
kumarak wrote:
> Sorry for interfering, I’m not an LLVM contributor or expert, but I have been toying with a more comprehensive approach the last few days. If nothing else, I hope to get some feedback for myself. :) Besides constant-time select, there are many more issues introduced by codegen.
>
> One related issue is that x86-isel may optimize a selection over two memory accesses into a selection over two pointers or two base pointer offsets. Subsequently, only the selected pointer will be dereferenced, introducing a condition-dependent memory accesses.
>
@mhaeuser, apologies for joining the discussion late, and thank you for outlining the metadata-based approach and highlighting the codegen concerns.
We believe the metadata-based design does not scale reliably for enforcing constant-time guarantees. As @nikic pointed out, LLVM transformations are generally free to drop metadata they do not explicitly understand or preserve. Since metadata is not part of core IR semantics, it can be lost during optimization, DAG combining, legalization, or backend lowering. This creates a real risk that secrecy annotations may silently disappear, along with the constant-time guarantees they were meant to enforce.
For this reason, we chose to introduce a dedicated pseudo-instruction. By encoding constant-time behavior as an explicit IR-level primitive, the semantics become part of the operation itself rather than an attached hint. The pseudo-instruction remains opaque to generic optimizations and can be lowered in the backend to a known-safe sequence of constant-time instructions, avoiding data-dependent control flow and unintended transformations.
There is also a clear separation between constant-time selection and constant-time memory behavior. The pointer-select/deref issue concerns memory semantics and should be addressed through separate contracts or primitives. Overloading select with memory-related semantics combines two problems and complicates reasoning about both.
Regarding DOIT/DIT flags, these are platform configuration concerns. We rely on developers to enable the appropriate architectural controls before executing constant-time code. The compiler should not attempt to implicitly toggle or synthesize those guarantees.
https://github.com/llvm/llvm-project/pull/166702
More information about the llvm-commits
mailing list