[llvm] [TableGen] Use bitwise operations to access HwMode ID. (PR #88377)
Jason Eckhardt via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 17 20:43:51 PDT 2024
================
@@ -290,11 +290,55 @@ CodeEmitterGen::getInstructionCases(Record *R, CodeGenTarget &Target) {
if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
const CodeGenHwModes &HWM = Target.getHwModes();
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
+ unsigned EncodingHwModesInBits = DefaultMode;
+ for (auto &[ModeId, Encoding] : EBM) {
+ // DefaultMode is 0, skip it.
+ if (ModeId != DefaultMode)
+ EncodingHwModesInBits |= (1 << (ModeId - 1));
+ }
+
+ // Get HwModes for this Instr by bitwise AND operations,
+ // and find the table to which this instr and hwmode belong.
+ append(" unsigned HwMode = "
+ "STI.getHwMode(MCSubtargetInfo::HwMode_EncodingInfo);\n");
+ append(" HwMode &= " + itostr(EncodingHwModesInBits) + ";\n");
+ append(" switch (HwMode) {\n");
+ append(" default: llvm_unreachable(\"Unknown hardware mode!\"); "
+ "break;\n");
+ for (auto &[ModeId, Encoding] : EBM) {
+ if (ModeId == DefaultMode) {
+ append(" case " + itostr(DefaultMode) +
+ ": InstBitsByHw = InstBits");
+ } else {
+ append(" case " + itostr(1 << (ModeId - 1)) +
----------------
nvjle wrote:
As mentioned earlier, I think there is a bug here with generating these case values. That is, the switch is on `HwMode`,which was assigned by the `getHwMode()` above it. Now as I understand it, `getHwMode` returns a HwMode integer value corresponding to a particular mode-- not a bit position in the bit vector. On the other hand, the case label is `1 << (ModeId - 1)`-- a bit position. It seems that we're comparing apples and oranges. Your test case wouldn't show this because the expressions happen to be equal for 0, 1, and 2. If you add another mode, I think you'll find it's case value is 4, not 3.
That would certainly explain at least some of the failures we're seeing downstream where we do indeed will see a `ModeId` larger than 2.
https://github.com/llvm/llvm-project/pull/88377
More information about the llvm-commits
mailing list