Skip to content

[CodeGen][NFC] Add laneBitmask as new MachineOperand Type #151944

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions llvm/docs/MIRLangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ For an int eq predicate ``ICMP_EQ``, the syntax is:
.. TODO: Describe the syntax of the metadata machine operands, and the
instructions debug ___location attribute.
.. TODO: Describe the syntax of the register live out machine operands.
.. TODO: Describe the syntax of the lanemask machine operands.
.. TODO: Describe the syntax of the machine memory operands.

Comments
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/CodeGen/MachineInstrBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ class MachineInstrBuilder {
return *this;
}

const MachineInstrBuilder &addLaneMask(LaneBitmask LaneMask) const {
MI->addOperand(*MF, MachineOperand::CreateLaneMask(LaneMask));
return *this;
}

const MachineInstrBuilder &addSym(MCSymbol *Sym,
unsigned char TargetFlags = 0) const {
MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
Expand Down
17 changes: 16 additions & 1 deletion llvm/include/llvm/CodeGen/MachineOperand.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/CodeGen/Register.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/MC/LaneBitmask.h"
#include "llvm/Support/Compiler.h"
#include <cassert>

Expand Down Expand Up @@ -69,7 +70,8 @@ class MachineOperand {
MO_Predicate, ///< Generic predicate for ISel
MO_ShuffleMask, ///< Other IR Constant for ISel (shuffle masks)
MO_DbgInstrRef, ///< Integer indices referring to an instruction+operand
MO_Last = MO_DbgInstrRef
MO_LaneMask, ///< Mask to represent active parts of registers
MO_Last = MO_LaneMask
};

private:
Expand Down Expand Up @@ -178,6 +180,7 @@ class MachineOperand {
Intrinsic::ID IntrinsicID; // For MO_IntrinsicID.
unsigned Pred; // For MO_Predicate
ArrayRef<int> ShuffleMask; // For MO_ShuffleMask
LaneBitmask LaneMask; // For MO_LaneMask

struct { // For MO_Register.
// Register number is in SmallContents.RegNo.
Expand Down Expand Up @@ -360,6 +363,7 @@ class MachineOperand {
bool isIntrinsicID() const { return OpKind == MO_IntrinsicID; }
bool isPredicate() const { return OpKind == MO_Predicate; }
bool isShuffleMask() const { return OpKind == MO_ShuffleMask; }
bool isLaneMask() const { return OpKind == MO_LaneMask; }
//===--------------------------------------------------------------------===//
// Accessors for Register Operands
//===--------------------------------------------------------------------===//
Expand Down Expand Up @@ -624,6 +628,11 @@ class MachineOperand {
return Contents.ShuffleMask;
}

LaneBitmask getLaneMask() const {
assert(isLaneMask() && "Wrong MachineOperand accessor");
return Contents.LaneMask;
}

/// Return the offset from the symbol in this operand. This always returns 0
/// for ExternalSymbol operands.
int64_t getOffset() const {
Expand Down Expand Up @@ -989,6 +998,12 @@ class MachineOperand {
return Op;
}

static MachineOperand CreateLaneMask(LaneBitmask LaneMask) {
MachineOperand Op(MachineOperand::MO_LaneMask);
Op.Contents.LaneMask = LaneMask;
return Op;
}

friend class MachineInstr;
friend class MachineRegisterInfo;

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/MIRParser/MILexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
.Case("constant-pool", MIToken::kw_constant_pool)
.Case("call-entry", MIToken::kw_call_entry)
.Case("custom", MIToken::kw_custom)
.Case("lanemask", MIToken::kw_lanemask)
.Case("liveout", MIToken::kw_liveout)
.Case("landing-pad", MIToken::kw_landing_pad)
.Case("inlineasm-br-indirect-target",
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/MIRParser/MILexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ struct MIToken {
kw_constant_pool,
kw_call_entry,
kw_custom,
kw_lanemask,
kw_liveout,
kw_landing_pad,
kw_inlineasm_br_indirect_target,
Expand Down
29 changes: 29 additions & 0 deletions llvm/lib/CodeGen/MIRParser/MIParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ class MIParser {
bool parseTargetIndexOperand(MachineOperand &Dest);
bool parseDbgInstrRefOperand(MachineOperand &Dest);
bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
bool parseLaneMaskOperand(MachineOperand &Dest);
bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
bool parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
MachineOperand &Dest,
Expand Down Expand Up @@ -2870,6 +2871,32 @@ bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
return false;
}

bool MIParser::parseLaneMaskOperand(MachineOperand &Dest) {
assert(Token.is(MIToken::kw_lanemask));

lex();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should get MIR print/parse tests and cover the error cases too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought the same as you did for MO_ShuffleMask, but don't know with which instruction it should go with( not associated yet to any kind of MI), in order to write test?

Copy link
Contributor

Choose a reason for hiding this comment

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

You could introduce the COPY_LANEMASK instruction here I suppose

if (expectAndConsume(MIToken::lparen))
return error("expected syntax lanemask(...)");

LaneBitmask LaneMask = LaneBitmask::getAll();
// Parse lanemask.
if (Token.isNot(MIToken::IntegerLiteral) && Token.isNot(MIToken::HexLiteral))
return error("expected a lane mask");
static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t),
"Use correct get-function for lane mask");
LaneBitmask::Type V;
if (getUint64(V))
return error("invalid lanemask value");
LaneMask = LaneBitmask(V);
lex();

if (expectAndConsume(MIToken::rparen))
return error("lanemask should be terminated by ')'.");

Dest = MachineOperand::CreateLaneMask(LaneMask);
return false;
}

bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
assert(Token.is(MIToken::kw_liveout));
uint32_t *Mask = MF.allocateRegMask();
Expand Down Expand Up @@ -2970,6 +2997,8 @@ bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx,
return parseIntrinsicOperand(Dest);
case MIToken::kw_target_index:
return parseTargetIndexOperand(Dest);
case MIToken::kw_lanemask:
return parseLaneMaskOperand(Dest);
case MIToken::kw_liveout:
return parseLiveoutRegisterMaskOperand(Dest);
case MIToken::kw_floatpred:
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/MIRPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,8 @@ static void printMIOperand(raw_ostream &OS, MFPrintState &State,
case MachineOperand::MO_Predicate:
case MachineOperand::MO_BlockAddress:
case MachineOperand::MO_DbgInstrRef:
case MachineOperand::MO_ShuffleMask: {
case MachineOperand::MO_ShuffleMask:
case MachineOperand::MO_LaneMask: {
unsigned TiedOperandIdx = 0;
if (ShouldPrintRegisterTies && Op.isReg() && Op.isTied() && !Op.isDef())
TiedOperandIdx = Op.getParent()->findTiedOperandIdx(OpIdx);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/MIRVRegNamerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ std::string VRegRenamer::getInstructionOpcodeHash(MachineInstr &MI) {
case MachineOperand::MO_ExternalSymbol:
case MachineOperand::MO_GlobalAddress:
case MachineOperand::MO_BlockAddress:
case MachineOperand::MO_LaneMask:
case MachineOperand::MO_RegisterMask:
case MachineOperand::MO_RegisterLiveOut:
case MachineOperand::MO_Metadata:
Expand Down
19 changes: 16 additions & 3 deletions llvm/lib/CodeGen/MachineOperand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
return getPredicate() == Other.getPredicate();
case MachineOperand::MO_ShuffleMask:
return getShuffleMask() == Other.getShuffleMask();
case MachineOperand::MO_LaneMask:
return getLaneMask() == Other.getLaneMask();
}
llvm_unreachable("Invalid machine operand type");
}
Expand Down Expand Up @@ -445,6 +447,9 @@ hash_code llvm::hash_value(const MachineOperand &MO) {
return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getPredicate());
case MachineOperand::MO_ShuffleMask:
return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getShuffleMask());
case MachineOperand::MO_LaneMask:
return hash_combine(MO.getType(), MO.getTargetFlags(),
MO.getLaneMask().getAsInteger());
}
llvm_unreachable("Invalid machine operand type");
}
Expand Down Expand Up @@ -1004,11 +1009,11 @@ void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
}
case MachineOperand::MO_Predicate: {
auto Pred = static_cast<CmpInst::Predicate>(getPredicate());
OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred("
<< Pred << ')';
OS << (CmpInst::isIntPredicate(Pred) ? "int" : "float") << "pred(" << Pred
<< ')';
break;
}
case MachineOperand::MO_ShuffleMask:
case MachineOperand::MO_ShuffleMask: {
OS << "shufflemask(";
ArrayRef<int> Mask = getShuffleMask();
StringRef Separator;
Expand All @@ -1023,6 +1028,14 @@ void MachineOperand::print(raw_ostream &OS, ModuleSlotTracker &MST,
OS << ')';
break;
}
case MachineOperand::MO_LaneMask: {
OS << "lanemask(";
LaneBitmask LaneMask = getLaneMask();
OS << "0x" << PrintLaneMask(LaneMask);
OS << ')';
break;
}
}
}

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/CodeGen/MachineStableHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ stable_hash llvm::stableHashValue(const MachineOperand &MO) {
return stable_hash_combine(MO.getType(), MO.getTargetFlags(),
stable_hash_name(SymbolName));
}
case MachineOperand::MO_LaneMask: {
return stable_hash_combine(MO.getType(), MO.getTargetFlags(),
MO.getLaneMask().getAsInteger());
}
case MachineOperand::MO_CFIIndex:
return stable_hash_combine(MO.getType(), MO.getTargetFlags(),
MO.getCFIIndex());
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ static bool IsAnAddressOperand(const MachineOperand &MO) {
return true;
case MachineOperand::MO_RegisterMask:
case MachineOperand::MO_RegisterLiveOut:
case MachineOperand::MO_LaneMask:
return false;
case MachineOperand::MO_Metadata:
case MachineOperand::MO_MCSymbol:
Expand Down
17 changes: 17 additions & 0 deletions llvm/unittests/CodeGen/MachineOperandTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,23 @@ TEST(MachineOperandTest, PrintGlobalAddress) {
}
}

TEST(MachineOperandTest, PrintLaneMask) {
// Create a MachineOperand with a lanemask and print it.
LaneBitmask LaneMask = LaneBitmask(12);
MachineOperand MO = MachineOperand::CreateLaneMask(LaneMask);

// Checking some preconditions on the newly created
// MachineOperand.
ASSERT_TRUE(MO.isLaneMask());
ASSERT_TRUE(MO.getLaneMask() == LaneMask);

std::string str;
// Print a MachineOperand that is lanemask as in HEX representation.
raw_string_ostream OS(str);
MO.print(OS, /*TRI=*/nullptr);
EXPECT_EQ(str, "lanemask(0x000000000000000C)");
}

TEST(MachineOperandTest, PrintRegisterLiveOut) {
// Create a MachineOperand with a register live out list and print it.
uint32_t Mask = 0;
Expand Down