Stream: cranelift

Topic: Matching dynamically sized constants using ISLE


view this post on Zulip Colton (May 06 2025 at 00:12):

I'm using ISLE to perform simplifications over an AST data structure, where the bit-width is arbitrary (ranging from 1..64). My ISLE definitions look something like this (minified):

;; Index into a vector of asts
(type index (primitive AstIdx))

(type SimpleAst extern
  (enum
  (Or (a index) (b index) )
  (Constant (c u64) (width u8) )
  ))

(decl Constant (u64 u8) SimpleAst)
(extern constructor Constant constant)

Let's say I want to apply the rewrite rule (a|-1) => a, my rule definition currently looks like:

(rule (lower (Ast.Or (Ast.Constant 18446744073709551615 width0) a))
    (Constant 18446744073709551615 width0)
)

, where 18446744073709551615 is the unsigned representation of -1 modulo 2**64. This is fine when the bit-width of the expression is 64 (specified by the width field), but for smaller but widths I need to match 18446744073709551615 reduced to the width of the expression (e.g. 255 for 8 bits).

What's the correct way to match a constant, when that constant needs to be truncated down to some dynamically specified bitwidth?


Last updated: Dec 06 2025 at 07:03 UTC