llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp:2425:24: warning: Assert statement modifies 'ExpectedID'. [assignmentInAssert]

Open dcb314 opened this issue 2 years ago • 5 comments

Source code is

  assert(ExpectedID++ == ID && "combine rules are not ordered!");

If asserts are switched off, this code might not work.

dcb314 avatar Apr 27 '24 08:04 dcb314

Looks like this variable is only used within the assert: https://github.com/llvm/llvm-project/blob/main/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp#L2419-L2428

void GICombinerEmitter::emitTestSimplePredicate(raw_ostream &OS) {
  if (!AllCombineRules.empty()) {
    OS << "enum {\n";
    std::string EnumeratorSeparator = " = GICXXPred_Invalid + 1,\n";
    // To avoid emitting a switch, we expect that all those rules are in order.
    // That way we can just get the RuleID from the enum by subtracting
    // (GICXXPred_Invalid + 1).
    unsigned ExpectedID = 0;
    (void)ExpectedID;
    for (const auto &ID : keys(AllCombineRules)) {
      assert(ExpectedID++ == ID && "combine rules are not ordered!");
      OS << "  " << getIsEnabledPredicateEnumName(ID) << EnumeratorSeparator;
      EnumeratorSeparator = ",\n";
    }
    OS << "};\n\n";
  }

  OS << "bool " << getClassName()
     << "::testSimplePredicate(unsigned Predicate) const {\n"
     << "    return RuleConfig.isRuleEnabled(Predicate - "
        "GICXXPred_Invalid - "
        "1);\n"
     << "}\n";
}

So I don't think this is a bug.

dc03-work avatar Apr 28 '24 12:04 dc03-work

@Pierre-vh

tschuett avatar Apr 28 '24 12:04 tschuett

Can we just pull the increment out of the assert? That would also avoid the need for the void cast to silence unused warning

arsenm avatar May 07 '24 20:05 arsenm

Can we just pull the increment out of the assert? That would also avoid the need for the void cast to silence unused warning

From what I can see the variable is used only within the assert, so that would add an unnecessary operation to the loop (unless you cover it with something like LLVM_DEBUG() I guess)

dc03-work avatar May 10 '24 04:05 dc03-work

From what I can see the variable is used only within the assert, so that would add an unnecessary operation to the loop (unless you cover it with something like LLVM_DEBUG() I guess)

Luckily we have compiler optimizations that can remove that for you

arsenm avatar May 10 '24 05:05 arsenm