From 498d83db6c77739e9c580ed296f035d8c3df0c93 Mon Sep 17 00:00:00 2001 From: Thomas Symalla Date: Wed, 4 Dec 2024 08:50:46 +0100 Subject: [PATCH] Use enum inside struct to avoid static_casts --- lib/TableGen/Operations.cpp | 14 +-- test/example/generated/ExampleDialect.cpp.inc | 108 +++++++++--------- test/example/generated/ExampleDialect.h.inc | 68 +++++------ 3 files changed, 95 insertions(+), 95 deletions(-) diff --git a/lib/TableGen/Operations.cpp b/lib/TableGen/Operations.cpp index feeb1b9..0320462 100644 --- a/lib/TableGen/Operations.cpp +++ b/lib/TableGen/Operations.cpp @@ -194,10 +194,10 @@ void OperationBase::emitArgumentAccessorDeclarations(llvm::raw_ostream &out, } if (!argNames.empty()) { - out << "enum class ArgumentIndex: uint32_t {\n"; + out << "struct ArgumentIndex { enum Value: uint32_t {\n"; for (const auto &[index, argName] : llvm::enumerate(argNames)) out << tgfmt("$0 = $1,\n", &fmt, argName, numSuperclassArgs + index); - out << "};"; + out << "};};"; } } @@ -216,7 +216,7 @@ void AccessorBuilder::emitGetterDefinition() const { if (!m_arg.type->isVarArgList()) { fromLlvm = tgfmt( - "getArgOperand(static_cast(ArgumentIndex::$Name))", &m_fmt); + "getArgOperand(ArgumentIndex::Value::$Name)", &m_fmt); if (auto *attr = dyn_cast(m_arg.type)) fromLlvm = tgfmt(attr->getFromLlvmValue(), &m_fmt, fromLlvm); else if (m_arg.type->isTypeArg()) @@ -224,7 +224,7 @@ void AccessorBuilder::emitGetterDefinition() const { } else { fromLlvm = tgfmt( R"(::llvm::make_range( - value_op_iterator(arg_begin() + static_cast(ArgumentIndex::$Name$0)), + value_op_iterator(arg_begin() + ArgumentIndex::Value::$Name$0), value_op_iterator(arg_end())))", &m_fmt, "Start"); } @@ -252,7 +252,7 @@ void AccessorBuilder::emitSetterDefinition() const { m_os << tgfmt(R"( void $_op::set$Name($cppType $name) { - setArgOperand(static_cast(ArgumentIndex::$Name), $toLlvm); + setArgOperand(ArgumentIndex::Value::$Name, $toLlvm); })", &m_fmt); } @@ -264,8 +264,8 @@ void AccessorBuilder::emitVarArgReplacementDefinition() const { $_op *$_op::replace$Name(::llvm::ArrayRef $name) { ::llvm::SmallVector newArgs; - if (static_cast(ArgumentIndex::$Name$0) > 0) - newArgs.append(arg_begin(), arg_begin() + static_cast(ArgumentIndex::$Name$0)); + if (ArgumentIndex::Value::$Name$0 > 0) + newArgs.append(arg_begin(), arg_begin() + ArgumentIndex::Value::$Name$0); newArgs.append($name.begin(), $name.end()); $_op *newOp = ::llvm::cast<$_op>(::llvm::CallInst::Create(getCalledFunction(), newArgs, this->getName(), this->getIterator())); newOp->copyMetadata(*this); diff --git a/test/example/generated/ExampleDialect.cpp.inc b/test/example/generated/ExampleDialect.cpp.inc index 5ae2744..30f4a73 100644 --- a/test/example/generated/ExampleDialect.cpp.inc +++ b/test/example/generated/ExampleDialect.cpp.inc @@ -322,25 +322,25 @@ return true; ::llvm::Value * StreamReduceOp::getPtr() const { - return getArgOperand(static_cast(ArgumentIndex::Ptr)); + return getArgOperand(ArgumentIndex::Value::Ptr); } void StreamReduceOp::setPtr(::llvm::Value * ptr) { - setArgOperand(static_cast(ArgumentIndex::Ptr), ptr); + setArgOperand(ArgumentIndex::Value::Ptr, ptr); } ::llvm::Value * StreamReduceOp::getCount() const { - return getArgOperand(static_cast(ArgumentIndex::Count)); + return getArgOperand(ArgumentIndex::Value::Count); } void StreamReduceOp::setCount(::llvm::Value * count) { - setArgOperand(static_cast(ArgumentIndex::Count), count); + setArgOperand(ArgumentIndex::Value::Count, count); } ::llvm::Value * StreamReduceOp::getInitial() const { - return getArgOperand(static_cast(ArgumentIndex::Initial)); + return getArgOperand(ArgumentIndex::Value::Initial); } void StreamReduceOp::setInitial(::llvm::Value * initial) { - setArgOperand(static_cast(ArgumentIndex::Initial), initial); + setArgOperand(ArgumentIndex::Value::Initial, initial); } const ::llvm::StringLiteral Add32Op::s_name{"xd.ir.add32"}; @@ -441,25 +441,25 @@ uint32_t const extra = getExtra(); ::llvm::Value * Add32Op::getLhs() const { - return getArgOperand(static_cast(ArgumentIndex::Lhs)); + return getArgOperand(ArgumentIndex::Value::Lhs); } void Add32Op::setLhs(::llvm::Value * lhs) { - setArgOperand(static_cast(ArgumentIndex::Lhs), lhs); + setArgOperand(ArgumentIndex::Value::Lhs, lhs); } ::llvm::Value * Add32Op::getRhs() const { - return getArgOperand(static_cast(ArgumentIndex::Rhs)); + return getArgOperand(ArgumentIndex::Value::Rhs); } void Add32Op::setRhs(::llvm::Value * rhs) { - setArgOperand(static_cast(ArgumentIndex::Rhs), rhs); + setArgOperand(ArgumentIndex::Value::Rhs, rhs); } uint32_t Add32Op::getExtra() const { - return ::llvm::cast<::llvm::ConstantInt>(getArgOperand(static_cast(ArgumentIndex::Extra)))->getZExtValue() ; + return ::llvm::cast<::llvm::ConstantInt>(getArgOperand(ArgumentIndex::Value::Extra))->getZExtValue() ; } void Add32Op::setExtra(uint32_t extra) { - setArgOperand(static_cast(ArgumentIndex::Extra), ::llvm::ConstantInt::get(::llvm::IntegerType::get(getContext(), 32), extra) ); + setArgOperand(ArgumentIndex::Value::Extra, ::llvm::ConstantInt::get(::llvm::IntegerType::get(getContext(), 32), extra) ); } ::llvm::Value *Add32Op::getResult() {return this;} @@ -543,18 +543,18 @@ rhs ::llvm::Value * CombineOp::getLhs() const { - return getArgOperand(static_cast(ArgumentIndex::Lhs)); + return getArgOperand(ArgumentIndex::Value::Lhs); } void CombineOp::setLhs(::llvm::Value * lhs) { - setArgOperand(static_cast(ArgumentIndex::Lhs), lhs); + setArgOperand(ArgumentIndex::Value::Lhs, lhs); } ::llvm::Value * CombineOp::getRhs() const { - return getArgOperand(static_cast(ArgumentIndex::Rhs)); + return getArgOperand(ArgumentIndex::Value::Rhs); } void CombineOp::setRhs(::llvm::Value * rhs) { - setArgOperand(static_cast(ArgumentIndex::Rhs), rhs); + setArgOperand(ArgumentIndex::Value::Rhs, rhs); } ::llvm::Value *CombineOp::getResult() {return this;} @@ -647,18 +647,18 @@ index ::llvm::Value * ExtractElementOp::getVector() const { - return getArgOperand(static_cast(ArgumentIndex::Vector)); + return getArgOperand(ArgumentIndex::Value::Vector); } void ExtractElementOp::setVector(::llvm::Value * vector) { - setArgOperand(static_cast(ArgumentIndex::Vector), vector); + setArgOperand(ArgumentIndex::Value::Vector, vector); } ::llvm::Value * ExtractElementOp::getIndex() const { - return getArgOperand(static_cast(ArgumentIndex::Index)); + return getArgOperand(ArgumentIndex::Value::Index); } void ExtractElementOp::setIndex(::llvm::Value * index) { - setArgOperand(static_cast(ArgumentIndex::Index), index); + setArgOperand(ArgumentIndex::Value::Index, index); } ::llvm::Value *ExtractElementOp::getResult() {return this;} @@ -824,11 +824,11 @@ source ::llvm::Value * FromFixedVectorOp::getSource() const { - return getArgOperand(static_cast(ArgumentIndex::Source)); + return getArgOperand(ArgumentIndex::Value::Source); } void FromFixedVectorOp::setSource(::llvm::Value * source) { - setArgOperand(static_cast(ArgumentIndex::Source), source); + setArgOperand(ArgumentIndex::Value::Source, source); } ::llvm::Value *FromFixedVectorOp::getResult() {return this;} @@ -984,11 +984,11 @@ source ::llvm::Value * IExtOp::getSource() const { - return getArgOperand(static_cast(ArgumentIndex::Source)); + return getArgOperand(ArgumentIndex::Value::Source); } void IExtOp::setSource(::llvm::Value * source) { - setArgOperand(static_cast(ArgumentIndex::Source), source); + setArgOperand(ArgumentIndex::Value::Source, source); } ::llvm::Value *IExtOp::getResult() {return this;} @@ -1082,11 +1082,11 @@ source ::llvm::Value * ITruncOp::getSource() const { - return getArgOperand(static_cast(ArgumentIndex::Source)); + return getArgOperand(ArgumentIndex::Value::Source); } void ITruncOp::setSource(::llvm::Value * source) { - setArgOperand(static_cast(ArgumentIndex::Source), source); + setArgOperand(ArgumentIndex::Value::Source, source); } ::llvm::Value *ITruncOp::getResult() {return this;} @@ -1156,7 +1156,7 @@ source bool ImmutableOp::getVal() const { - return ::llvm::cast<::llvm::ConstantInt>(getArgOperand(static_cast(ArgumentIndex::Val)))->getZExtValue() ; + return ::llvm::cast<::llvm::ConstantInt>(getArgOperand(ArgumentIndex::Value::Val))->getZExtValue() ; } @@ -1259,25 +1259,25 @@ index ::llvm::Value * InsertElementOp::getVector() const { - return getArgOperand(static_cast(ArgumentIndex::Vector)); + return getArgOperand(ArgumentIndex::Value::Vector); } void InsertElementOp::setVector(::llvm::Value * vector) { - setArgOperand(static_cast(ArgumentIndex::Vector), vector); + setArgOperand(ArgumentIndex::Value::Vector, vector); } ::llvm::Value * InsertElementOp::getValue() const { - return getArgOperand(static_cast(ArgumentIndex::Value)); + return getArgOperand(ArgumentIndex::Value::Value); } void InsertElementOp::setValue(::llvm::Value * value) { - setArgOperand(static_cast(ArgumentIndex::Value), value); + setArgOperand(ArgumentIndex::Value::Value, value); } ::llvm::Value * InsertElementOp::getIndex() const { - return getArgOperand(static_cast(ArgumentIndex::Index)); + return getArgOperand(ArgumentIndex::Value::Index); } void InsertElementOp::setIndex(::llvm::Value * index) { - setArgOperand(static_cast(ArgumentIndex::Index), index); + setArgOperand(ArgumentIndex::Value::Index, index); } ::llvm::Value *InsertElementOp::getResult() {return this;} @@ -1350,18 +1350,18 @@ instName_0 ::llvm::Value * InstNameConflictDoubleOp::getInstName() const { - return getArgOperand(static_cast(ArgumentIndex::InstName)); + return getArgOperand(ArgumentIndex::Value::InstName); } void InstNameConflictDoubleOp::setInstName(::llvm::Value * instName) { - setArgOperand(static_cast(ArgumentIndex::InstName), instName); + setArgOperand(ArgumentIndex::Value::InstName, instName); } ::llvm::Value * InstNameConflictDoubleOp::getInstName_0() const { - return getArgOperand(static_cast(ArgumentIndex::InstName_0)); + return getArgOperand(ArgumentIndex::Value::InstName_0); } void InstNameConflictDoubleOp::setInstName_0(::llvm::Value * instName_0) { - setArgOperand(static_cast(ArgumentIndex::InstName_0), instName_0); + setArgOperand(ArgumentIndex::Value::InstName_0, instName_0); } ::llvm::Value *InstNameConflictDoubleOp::getResult() {return this;} @@ -1431,11 +1431,11 @@ instName ::llvm::Value * InstNameConflictOp::getInstName() const { - return getArgOperand(static_cast(ArgumentIndex::InstName)); + return getArgOperand(ArgumentIndex::Value::InstName); } void InstNameConflictOp::setInstName(::llvm::Value * instName) { - setArgOperand(static_cast(ArgumentIndex::InstName), instName); + setArgOperand(ArgumentIndex::Value::InstName, instName); } ::llvm::Value *InstNameConflictOp::getResult() {return this;} @@ -1500,14 +1500,14 @@ instName ::llvm::iterator_range<::llvm::User::value_op_iterator> InstNameConflictVarargsOp::getInstName_0() { return ::llvm::make_range( - value_op_iterator(arg_begin() + static_cast(ArgumentIndex::InstName_0Start)), + value_op_iterator(arg_begin() + ArgumentIndex::Value::InstName_0Start), value_op_iterator(arg_end())); } InstNameConflictVarargsOp *InstNameConflictVarargsOp::replaceInstName_0(::llvm::ArrayRef instName_0) { ::llvm::SmallVector newArgs; - if (static_cast(ArgumentIndex::InstName_0Start) > 0) - newArgs.append(arg_begin(), arg_begin() + static_cast(ArgumentIndex::InstName_0Start)); + if (ArgumentIndex::Value::InstName_0Start > 0) + newArgs.append(arg_begin(), arg_begin() + ArgumentIndex::Value::InstName_0Start); newArgs.append(instName_0.begin(), instName_0.end()); InstNameConflictVarargsOp *newOp = ::llvm::cast(::llvm::CallInst::Create(getCalledFunction(), newArgs, this->getName(), this->getIterator())); newOp->copyMetadata(*this); @@ -1687,11 +1687,11 @@ data ::llvm::Value * SetWriteOp::getData() const { - return getArgOperand(static_cast(ArgumentIndex::Data)); + return getArgOperand(ArgumentIndex::Value::Data); } void SetWriteOp::setData(::llvm::Value * data) { - setArgOperand(static_cast(ArgumentIndex::Data), data); + setArgOperand(ArgumentIndex::Value::Data, data); } @@ -1760,11 +1760,11 @@ data ::llvm::Type * SizeOfOp::getSizeofType() const { - return getArgOperand(static_cast(ArgumentIndex::SizeofType))->getType(); + return getArgOperand(ArgumentIndex::Value::SizeofType)->getType(); } void SizeOfOp::setSizeofType(::llvm::Type * sizeof_type) { - setArgOperand(static_cast(ArgumentIndex::SizeofType), llvm::PoisonValue::get(sizeof_type)); + setArgOperand(ArgumentIndex::Value::SizeofType, llvm::PoisonValue::get(sizeof_type)); } ::llvm::Value *SizeOfOp::getResult() {return this;} @@ -2110,7 +2110,7 @@ initial ::llvm::StringRef StringAttrOp::getVal() const { - return ::llvm::cast<::llvm::ConstantDataArray>(::llvm::cast<::llvm::GlobalVariable>(getArgOperand(static_cast(ArgumentIndex::Val)))->getInitializer())->getAsString() ; + return ::llvm::cast<::llvm::ConstantDataArray>(::llvm::cast<::llvm::GlobalVariable>(getArgOperand(ArgumentIndex::Value::Val))->getInitializer())->getAsString() ; } @@ -2169,11 +2169,11 @@ data ::llvm::Value * WriteOp::getData() const { - return getArgOperand(static_cast(ArgumentIndex::Data)); + return getArgOperand(ArgumentIndex::Value::Data); } void WriteOp::setData(::llvm::Value * data) { - setArgOperand(static_cast(ArgumentIndex::Data), data); + setArgOperand(ArgumentIndex::Value::Data, data); } @@ -2234,22 +2234,22 @@ data ::llvm::Value * WriteVarArgOp::getData() const { - return getArgOperand(static_cast(ArgumentIndex::Data)); + return getArgOperand(ArgumentIndex::Value::Data); } void WriteVarArgOp::setData(::llvm::Value * data) { - setArgOperand(static_cast(ArgumentIndex::Data), data); + setArgOperand(ArgumentIndex::Value::Data, data); } ::llvm::iterator_range<::llvm::User::value_op_iterator> WriteVarArgOp::getArgs() { return ::llvm::make_range( - value_op_iterator(arg_begin() + static_cast(ArgumentIndex::ArgsStart)), + value_op_iterator(arg_begin() + ArgumentIndex::Value::ArgsStart), value_op_iterator(arg_end())); } WriteVarArgOp *WriteVarArgOp::replaceArgs(::llvm::ArrayRef args) { ::llvm::SmallVector newArgs; - if (static_cast(ArgumentIndex::ArgsStart) > 0) - newArgs.append(arg_begin(), arg_begin() + static_cast(ArgumentIndex::ArgsStart)); + if (ArgumentIndex::Value::ArgsStart > 0) + newArgs.append(arg_begin(), arg_begin() + ArgumentIndex::Value::ArgsStart); newArgs.append(args.begin(), args.end()); WriteVarArgOp *newOp = ::llvm::cast(::llvm::CallInst::Create(getCalledFunction(), newArgs, this->getName(), this->getIterator())); newOp->copyMetadata(*this); diff --git a/test/example/generated/ExampleDialect.h.inc b/test/example/generated/ExampleDialect.h.inc index 9e5794f..9c7ef34 100644 --- a/test/example/generated/ExampleDialect.h.inc +++ b/test/example/generated/ExampleDialect.h.inc @@ -104,11 +104,11 @@ uint32_t getNumElements() const; void setCount(::llvm::Value * count); ::llvm::Value * getInitial() const; void setInitial(::llvm::Value * initial); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Ptr = 0, Count = 1, Initial = 2, -}; +};}; }; class Add32Op : public ::llvm::CallInst { @@ -132,11 +132,11 @@ bool verifier(::llvm::raw_ostream &errs); void setRhs(::llvm::Value * rhs); uint32_t getExtra() const; void setExtra(uint32_t extra); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Lhs = 0, Rhs = 1, Extra = 2, -}; +};}; ::llvm::Value * getResult(); @@ -161,10 +161,10 @@ bool verifier(::llvm::raw_ostream &errs); void setLhs(::llvm::Value * lhs); ::llvm::Value * getRhs() const; void setRhs(::llvm::Value * rhs); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Lhs = 0, Rhs = 1, -}; +};}; ::llvm::Value * getResult(); @@ -189,10 +189,10 @@ bool verifier(::llvm::raw_ostream &errs); void setVector(::llvm::Value * vector); ::llvm::Value * getIndex() const; void setIndex(::llvm::Value * index); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Vector = 0, Index = 1, -}; +};}; ::llvm::Value * getResult(); @@ -215,9 +215,9 @@ bool verifier(::llvm::raw_ostream &errs); ::llvm::Value * getSource() const; void setSource(::llvm::Value * source); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Source = 0, -}; +};}; ::llvm::Value * getResult(); @@ -261,9 +261,9 @@ bool verifier(::llvm::raw_ostream &errs); ::llvm::Value * getSource() const; void setSource(::llvm::Value * source); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Source = 0, -}; +};}; ::llvm::Value * getResult(); @@ -286,9 +286,9 @@ bool verifier(::llvm::raw_ostream &errs); ::llvm::Value * getSource() const; void setSource(::llvm::Value * source); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Source = 0, -}; +};}; ::llvm::Value * getResult(); @@ -309,9 +309,9 @@ Source = 0, bool verifier(::llvm::raw_ostream &errs); -bool getVal() const;enum class ArgumentIndex: uint32_t { +bool getVal() const;struct ArgumentIndex { enum Value: uint32_t { Val = 0, -}; +};}; }; @@ -337,11 +337,11 @@ bool verifier(::llvm::raw_ostream &errs); void setValue(::llvm::Value * value); ::llvm::Value * getIndex() const; void setIndex(::llvm::Value * index); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Vector = 0, Value = 1, Index = 2, -}; +};}; ::llvm::Value * getResult(); @@ -366,10 +366,10 @@ bool verifier(::llvm::raw_ostream &errs); void setInstName(::llvm::Value * instName); ::llvm::Value * getInstName_0() const; void setInstName_0(::llvm::Value * instName_0); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { InstName = 0, InstName_0 = 1, -}; +};}; ::llvm::Value * getResult(); @@ -392,9 +392,9 @@ bool verifier(::llvm::raw_ostream &errs); ::llvm::Value * getInstName() const; void setInstName(::llvm::Value * instName); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { InstName = 0, -}; +};}; ::llvm::Value * getResult(); @@ -419,9 +419,9 @@ bool verifier(::llvm::raw_ostream &errs); /// Returns a new op with the same arguments and a new tail argument list. /// The object on which this is called will be replaced and erased. InstNameConflictVarargsOp *replaceInstName_0(::llvm::ArrayRef); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { InstName_0Start = 0, -}; +};}; ::llvm::Value * getResult(); @@ -486,9 +486,9 @@ bool verifier(::llvm::raw_ostream &errs); ::llvm::Value * getData() const; void setData(::llvm::Value * data); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Data = 0, -}; +};}; }; @@ -510,9 +510,9 @@ bool verifier(::llvm::raw_ostream &errs); ::llvm::Type * getSizeofType() const; void setSizeofType(::llvm::Type * sizeof_type); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { SizeofType = 0, -}; +};}; ::llvm::Value * getResult(); @@ -596,9 +596,9 @@ bool verifier(::llvm::raw_ostream &errs); bool verifier(::llvm::raw_ostream &errs); -::llvm::StringRef getVal() const;enum class ArgumentIndex: uint32_t { +::llvm::StringRef getVal() const;struct ArgumentIndex { enum Value: uint32_t { Val = 0, -}; +};}; }; @@ -620,9 +620,9 @@ bool verifier(::llvm::raw_ostream &errs); ::llvm::Value * getData() const; void setData(::llvm::Value * data); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Data = 0, -}; +};}; }; @@ -648,10 +648,10 @@ bool verifier(::llvm::raw_ostream &errs); /// Returns a new op with the same arguments and a new tail argument list. /// The object on which this is called will be replaced and erased. WriteVarArgOp *replaceArgs(::llvm::ArrayRef); - enum class ArgumentIndex: uint32_t { + struct ArgumentIndex { enum Value: uint32_t { Data = 0, ArgsStart = 1, -}; +};}; };