How can I insert a attrStmt before a Call?

Suppose we have:
Expr Mutate_(const Call* op, const Expr& e) final{
return IRMutator::Mutate_(op, e);
}
This is a special call, like an intrinsic for a target, and we need insert a attrStmt before this call for some reason,
is there any handy function in tvm/halide to do that?

Thanks,

You need to build a mutator that insert AttrStmt that wraps the statement containing that call. Since call itself is not a statement, the best way I can think of is to set a flag after you see the call, and overload Mutate(const Stmt &op) to wrap AttrStmt when you see the flag in post order

Do you mean: I build a map<noderef, flag> for call expr first, and then insert the AtrrStmt with Call expr inside via PostOrderVisit?

I just found this with you information, it’s what i want. thanks

// inject the operator’s realization on the stmt.
class InjectAttach : public IRMutator {
public:
InjectAttach(const Stage& stage,
const Stage& attach_spec,
const std::unordered_map<IterVar, Range>& dom_map)
: stage_(stage), attach_spec_(attach_spec), dom_map_(dom_map) {}

Stmt Mutate(Stmt stmt) final {
CHECK(stmt.defined());
stmt = IRMutator::Mutate(stmt);
const AttrStmt* op = stmt.as();
if (op != nullptr &&
op->attr_key == attr::loop_scope) {
if (attach_spec_->attach_type == kScope &&
op->node == attach_spec_->attach_ivar) {
CHECK(!found_attach)
<< “Find IterVar” << attach_spec_->attach_ivar
<< " in multiple places in the IR";
found_attach = true;
stmt = AttrStmt::make(
op->node, op->attr_key, op->value,
MakePipeline(stage_, dom_map_, op->body, true));
}
}
return stmt;
}
// whether attach point is found
bool found_attach{false};

private:
};

Stmt Mutate_(const Evaluate* op, const Stmt& s) final{
     const Call* insn = op->value.as<Call>();
     if (insn != nullptr) {
         Stmt stmt = emit_pip_(pip);
         const AttrStmt* op = stmt.as<AttrStmt>();
         stmt = AttrStmt::make(op->node, op->attr_key, op->value, s);
         return stmt;
       }
     }
     return IRMutator::Mutate_(op, s);
   }

This is final code i’m using, for guys who may need it