Skip to content

[TableGen] Use bitwise operations to access HwMode ID. #88377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion llvm/include/llvm/MC/MCSubtargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,32 @@ class MCSubtargetInfo {
return ProcFeatures;
}

virtual unsigned getHwMode() const { return 0; }
/// HwMode IDs are stored and accessed in a bit set format, enabling
/// users to efficiently retrieve specific IDs, such as the RegInfo
/// HwMode ID, from the set as required. Using this approach, various
/// types of HwMode IDs can be added to a subtarget to manage different
/// attributes within that subtarget, significantly enhancing the
/// scalability and usability of HwMode. Moreover, to ensure compatibility,
/// this method also supports controlling multiple attributes with a single
/// HwMode ID, just as was done previously.
enum HwModeType {
HwMode_Default, // Return the smallest HwMode ID of current subtarget.
HwMode_ValueType, // Return the HwMode ID that controls the ValueType.
HwMode_RegInfo, // Return the HwMode ID that controls the RegSizeInfo and
// SubRegRange.
HwMode_EncodingInfo // Return the HwMode ID that controls the EncodingInfo.
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there are now two APIs, please document each one specifically and clearly. Minimally, something along the lines of "Returns a bit vector/set of all hardware modes." for the first, and "Returns a single hardware mode of the requested type."

/// Return a bit set containing all HwMode IDs of the current subtarget.
virtual unsigned getHwModeSet() const { return 0; }

/// HwMode ID corresponding to the 'type' parameter is retrieved from the
/// HwMode bit set of the current subtarget. It’s important to note that if
/// the current subtarget possesses two HwMode IDs and both control a single
/// attribute (such as RegInfo), this interface will result in an error.
virtual unsigned getHwMode(enum HwModeType type = HwMode_Default) const {
return 0;
}

/// Return the cache size in bytes for the given level of cache.
/// Level is zero-based, so a value of zero means the first level of
Expand Down
162 changes: 162 additions & 0 deletions llvm/test/TableGen/HwModeBitSet.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// This is to test the scenario where different HwMode attributes coexist.
// RUN: llvm-tblgen -gen-register-info -register-info-debug -I %p/../../include %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=CHECK-REG
// RUN: llvm-tblgen -gen-subtarget -I %p/../../include %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-SUBTARGET


include "llvm/Target/Target.td"

def TestTargetInstrInfo : InstrInfo;

def TestTarget : Target {
let InstructionSet = TestTargetInstrInfo;
}

def TestMode : HwMode<"+feat", []>;
def TestMode1 : HwMode<"+feat1", []>;
def TestMode2 : HwMode<"+feat2", []>;

class MyReg<string n>
: Register<n> {
let Namespace = "Test";
}

class MyClass<int size, list<ValueType> types, dag registers>
: RegisterClass<"Test", types, size, registers> {
let Size = size;
}

def X0 : MyReg<"x0">;
def X1 : MyReg<"x1">;
def X2 : MyReg<"x2">;
def X3 : MyReg<"x3">;
def X4 : MyReg<"x4">;
def X5 : MyReg<"x5">;
def X6 : MyReg<"x6">;
def X7 : MyReg<"x7">;
def X8 : MyReg<"x8">;
def X9 : MyReg<"x9">;
def X10 : MyReg<"x10">;
def X11 : MyReg<"x11">;
def X12 : MyReg<"x12">;
def X13 : MyReg<"x13">;
def X14 : MyReg<"x14">;
def X15 : MyReg<"x15">;

def ValueModeVT : ValueTypeByHwMode<[DefaultMode, TestMode, TestMode1],
[i32, i64, f32]>;

let RegInfos = RegInfoByHwMode<[DefaultMode, TestMode],
[RegInfo<32,32,32>, RegInfo<64,64,64>]> in
def XRegs : MyClass<32, [ValueModeVT], (sequence "X%u", 0, 15)>;

def sub_even : SubRegIndex<32> {
let SubRegRanges = SubRegRangeByHwMode<[DefaultMode, TestMode],
[SubRegRange<32>, SubRegRange<64>]>;
}
def sub_odd : SubRegIndex<32, 32> {
let SubRegRanges = SubRegRangeByHwMode<[DefaultMode, TestMode],
[SubRegRange<32, 32>, SubRegRange<64, 64>]>;
}

def XPairs : RegisterTuples<[sub_even, sub_odd],
[(decimate (rotl XRegs, 0), 2),
(decimate (rotl XRegs, 1), 2)]>;

let RegInfos = RegInfoByHwMode<[DefaultMode, TestMode],
[RegInfo<64,64,32>, RegInfo<128,128,64>]> in
def XPairsClass : MyClass<64, [untyped], (add XPairs)>;

// Modes who are not controlling Register related features will be manipulated
// the same as DefaultMode.
// CHECK-REG-LABEL: RegisterClass XRegs:
// CHECK-REG: SpillSize: { Default:32 TestMode:64 TestMode1:32 TestMode2:32 }
// CHECK-REG: SpillAlignment: { Default:32 TestMode:64 TestMode1:32 TestMode2:32 }
// CHECK-REG: Regs: X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15

// CHECK-REG-LABEL: RegisterClass XPairsClass:
// CHECK-REG: SpillSize: { Default:64 TestMode:128 TestMode1:64 TestMode2:64 }
// CHECK-REG: SpillAlignment: { Default:32 TestMode:64 TestMode1:32 TestMode2:32 }
// CHECK-REG: CoveredBySubRegs: 1
// CHECK-REG: Regs: X0_X1 X2_X3 X4_X5 X6_X7 X8_X9 X10_X11 X12_X13 X14_X15

// CHECK-REG-LABEL: SubRegIndex sub_even:
// CHECK-REG: Offset: { Default:0 TestMode:0 TestMode1:0 TestMode2:0 }
// CHECK-REG: Size: { Default:32 TestMode:64 TestMode1:32 TestMode2:32 }
// CHECK-REG-LABEL: SubRegIndex sub_odd:
// CHECK-REG: Offset: { Default:32 TestMode:64 TestMode1:32 TestMode2:32 }
// CHECK-REG: Size: { Default:32 TestMode:64 TestMode1:32 TestMode2:32 }

//============================================================================//
//--------------------- Encoding/Decoding parts ------------------------------//
//============================================================================//
def fooTypeEncDefault : InstructionEncoding {
let Size = 8;
field bits<64> SoftFail = 0;
bits<64> Inst;
bits<8> factor;
let Inst{7...0} = factor;
let Inst{3...2} = 0b10;
let Inst{1...0} = 0b00;
}

def fooTypeEncA : InstructionEncoding {
let Size = 4;
field bits<32> SoftFail = 0;
bits<32> Inst;
bits<8> factor;
let Inst{7...0} = factor;
let Inst{3...2} = 0b11;
let Inst{1...0} = 0b00;
}


def foo : Instruction {
bits<32> Inst;
let OutOperandList = (outs);
let InOperandList = (ins i32imm:$factor);
let EncodingInfos = EncodingByHwMode<
[TestMode2, DefaultMode], [fooTypeEncA, fooTypeEncDefault]
>;
let AsmString = "foo $factor";
}

// CHECK-SUBTARGET-LABEL: unsigned TestTargetGenSubtargetInfo::getHwModeSet() const {
// CHECK-SUBTARGET: unsigned Modes = 0;
// CHECK-SUBTARGET: if (checkFeatures("+feat")) Modes |= (1 << 0);
// CHECK-SUBTARGET: if (checkFeatures("+feat1")) Modes |= (1 << 1);
// CHECK-SUBTARGET: if (checkFeatures("+feat2")) Modes |= (1 << 2);
// CHECK-SUBTARGET: return Modes;
// CHECK-SUBTARGET: }
// CHECK-SUBTARGET-LABEL: unsigned TestTargetGenSubtargetInfo::getHwMode(enum HwModeType type) const {
// CHECK-SUBTARGET: unsigned Modes = getHwModeSet();
// CHECK-SUBTARGET: if (!Modes)
// CHECK-SUBTARGET: return Modes;
// CHECK-SUBTARGET: switch (type) {
// CHECK-SUBTARGET: case HwMode_Default:
// CHECK-SUBTARGET: return llvm::countr_zero(Modes) + 1;
// CHECK-SUBTARGET: case HwMode_ValueType:
// CHECK-SUBTARGET: Modes &= 3;
// CHECK-SUBTARGET: if (!Modes)
// CHECK-SUBTARGET: return Modes;
// CHECK-SUBTARGET: if (!llvm::has_single_bit<unsigned>(Modes))
// CHECK-SUBTARGET: llvm_unreachable("Two or more HwModes for ValueType were found!");
// CHECK-SUBTARGET: return llvm::countr_zero(Modes) + 1;
// CHECK-SUBTARGET: case HwMode_RegInfo:
// CHECK-SUBTARGET: Modes &= 1;
// CHECK-SUBTARGET: if (!Modes)
// CHECK-SUBTARGET: return Modes;
// CHECK-SUBTARGET: if (!llvm::has_single_bit<unsigned>(Modes))
// CHECK-SUBTARGET: llvm_unreachable("Two or more HwModes for RegInfo were found!");
// CHECK-SUBTARGET: return llvm::countr_zero(Modes) + 1;
// CHECK-SUBTARGET: case HwMode_EncodingInfo:
// CHECK-SUBTARGET: Modes &= 4;
// CHECK-SUBTARGET: if (!Modes)
// CHECK-SUBTARGET: return Modes;
// CHECK-SUBTARGET: if (!llvm::has_single_bit<unsigned>(Modes))
// CHECK-SUBTARGET: llvm_unreachable("Two or more HwModes for EncodingInfo were found!");
// CHECK-SUBTARGET: return llvm::countr_zero(Modes) + 1;
// CHECK-SUBTARGET: }
// CHECK-SUBTARGET: llvm_unreachable("unexpected HwModeType");
// CHECK-SUBTARGET: return 0; // should not get here
// CHECK-SUBTARGET: }

Loading