Skip to main content

cubecl_cpp/hip/
builtin.rs

1use cubecl_core::ir::{Scope, dialect::general::ReadBuiltinOp, prelude::*};
2
3use crate::{
4    shared::builtin::{LowerBuiltins, SharedBuiltin},
5    target::Hip,
6};
7
8impl MatchRewrite for LowerBuiltins<Hip> {
9    fn r#match(&mut self, ctx: &Context, op: Ptr<Operation>) -> bool {
10        op.is_op::<ReadBuiltinOp>(ctx)
11    }
12
13    fn rewrite(
14        &mut self,
15        ctx: &mut Context,
16        rewriter: &mut MatchRewriter,
17        op: Ptr<Operation>,
18    ) -> Result<()> {
19        let builtin = op.as_op::<ReadBuiltinOp>(ctx).unwrap().builtin(ctx).0;
20        let scope = Scope::from_context_and_inserter(ctx, rewriter);
21        if let Some(new_value) = builtin.maybe_lower_shared(&scope) {
22            rewriter.replace_operation_with_values(ctx, op, vec![new_value]);
23        }
24        Ok(())
25    }
26}