iree-org / iree

A retargetable MLIR-based machine learning compiler and runtime toolkit.

Home Page:http://iree.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Should use `verifySymbolUses` instead of open-coded verification.

stellaraccident opened this issue · comments

We have a number of cases where we are doing symbol verification in an unsafe/inefficient way directly in the verify method. We should be using verifySymbolUses by implementing DeclareOpInterfaceMethods<SymbolUserOpInterface> to do this instead.

While in there, we should also refactor accessors like getGlobal() to instead take a SymbolTableCollection &, which allows more efficient (cached) access which avoids repeated linear scans.

This affects Util Global ops (likely among others).

Example from upstream with it done right:

GlobalOp GlobalLoadConstOp::getGlobalOp(SymbolTableCollection &symbolTable) {
  return symbolTable.lookupNearestSymbolFrom<GlobalOp>(
      getOperation()->getParentOp(), getGlobalAttr());
}

LogicalResult
GlobalLoadConstOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
  GlobalOp referrent = getGlobalOp(symbolTable);
  if (!referrent)
    return emitOpError() << "undefined global: " << getGlobal();

  if (referrent.getIsMutable())
    return emitOpError() << "cannot load as const from mutable global "
                         << getGlobal();

  if (referrent.getType() != getResult().getType())
    return emitOpError() << "cannot load from global typed "
                         << referrent.getType() << " as "
                         << getResult().getType();

  return success();
}