Skip to content

Commit

Permalink
Use enum inside struct to avoid static_casts
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Symalla committed Dec 4, 2024
1 parent baabbc4 commit 498d83d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 95 deletions.
14 changes: 7 additions & 7 deletions lib/TableGen/Operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << "};};";
}
}

Expand All @@ -216,15 +216,15 @@ void AccessorBuilder::emitGetterDefinition() const {

if (!m_arg.type->isVarArgList()) {
fromLlvm = tgfmt(
"getArgOperand(static_cast<uint32_t>(ArgumentIndex::$Name))", &m_fmt);
"getArgOperand(ArgumentIndex::Value::$Name)", &m_fmt);
if (auto *attr = dyn_cast<Attr>(m_arg.type))
fromLlvm = tgfmt(attr->getFromLlvmValue(), &m_fmt, fromLlvm);
else if (m_arg.type->isTypeArg())
fromLlvm += "->getType()";
} else {
fromLlvm = tgfmt(
R"(::llvm::make_range(
value_op_iterator(arg_begin() + static_cast<uint32_t>(ArgumentIndex::$Name$0)),
value_op_iterator(arg_begin() + ArgumentIndex::Value::$Name$0),
value_op_iterator(arg_end())))",
&m_fmt, "Start");
}
Expand Down Expand Up @@ -252,7 +252,7 @@ void AccessorBuilder::emitSetterDefinition() const {
m_os << tgfmt(R"(
void $_op::set$Name($cppType $name) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::$Name), $toLlvm);
setArgOperand(ArgumentIndex::Value::$Name, $toLlvm);
})",
&m_fmt);
}
Expand All @@ -264,8 +264,8 @@ void AccessorBuilder::emitVarArgReplacementDefinition() const {
$_op *$_op::replace$Name(::llvm::ArrayRef<Value *> $name) {
::llvm::SmallVector<Value *> newArgs;
if (static_cast<uint32_t>(ArgumentIndex::$Name$0) > 0)
newArgs.append(arg_begin(), arg_begin() + static_cast<uint32_t>(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);
Expand Down
108 changes: 54 additions & 54 deletions test/example/generated/ExampleDialect.cpp.inc
Original file line number Diff line number Diff line change
Expand Up @@ -322,25 +322,25 @@ return true;


::llvm::Value * StreamReduceOp::getPtr() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Ptr));
return getArgOperand(ArgumentIndex::Value::Ptr);
}

void StreamReduceOp::setPtr(::llvm::Value * ptr) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Ptr), ptr);
setArgOperand(ArgumentIndex::Value::Ptr, ptr);
}
::llvm::Value * StreamReduceOp::getCount() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Count));
return getArgOperand(ArgumentIndex::Value::Count);
}

void StreamReduceOp::setCount(::llvm::Value * count) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Count), count);
setArgOperand(ArgumentIndex::Value::Count, count);
}
::llvm::Value * StreamReduceOp::getInitial() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Initial));
return getArgOperand(ArgumentIndex::Value::Initial);
}

void StreamReduceOp::setInitial(::llvm::Value * initial) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Initial), initial);
setArgOperand(ArgumentIndex::Value::Initial, initial);
}

const ::llvm::StringLiteral Add32Op::s_name{"xd.ir.add32"};
Expand Down Expand Up @@ -441,25 +441,25 @@ uint32_t const extra = getExtra();


::llvm::Value * Add32Op::getLhs() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Lhs));
return getArgOperand(ArgumentIndex::Value::Lhs);
}

void Add32Op::setLhs(::llvm::Value * lhs) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Lhs), lhs);
setArgOperand(ArgumentIndex::Value::Lhs, lhs);
}
::llvm::Value * Add32Op::getRhs() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Rhs));
return getArgOperand(ArgumentIndex::Value::Rhs);
}

void Add32Op::setRhs(::llvm::Value * rhs) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Rhs), rhs);
setArgOperand(ArgumentIndex::Value::Rhs, rhs);
}
uint32_t Add32Op::getExtra() const {
return ::llvm::cast<::llvm::ConstantInt>(getArgOperand(static_cast<uint32_t>(ArgumentIndex::Extra)))->getZExtValue() ;
return ::llvm::cast<::llvm::ConstantInt>(getArgOperand(ArgumentIndex::Value::Extra))->getZExtValue() ;
}

void Add32Op::setExtra(uint32_t extra) {
setArgOperand(static_cast<uint32_t>(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;}

Expand Down Expand Up @@ -543,18 +543,18 @@ rhs


::llvm::Value * CombineOp::getLhs() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Lhs));
return getArgOperand(ArgumentIndex::Value::Lhs);
}

void CombineOp::setLhs(::llvm::Value * lhs) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Lhs), lhs);
setArgOperand(ArgumentIndex::Value::Lhs, lhs);
}
::llvm::Value * CombineOp::getRhs() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Rhs));
return getArgOperand(ArgumentIndex::Value::Rhs);
}

void CombineOp::setRhs(::llvm::Value * rhs) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Rhs), rhs);
setArgOperand(ArgumentIndex::Value::Rhs, rhs);
}
::llvm::Value *CombineOp::getResult() {return this;}

Expand Down Expand Up @@ -647,18 +647,18 @@ index


::llvm::Value * ExtractElementOp::getVector() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Vector));
return getArgOperand(ArgumentIndex::Value::Vector);
}

void ExtractElementOp::setVector(::llvm::Value * vector) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Vector), vector);
setArgOperand(ArgumentIndex::Value::Vector, vector);
}
::llvm::Value * ExtractElementOp::getIndex() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Index));
return getArgOperand(ArgumentIndex::Value::Index);
}

void ExtractElementOp::setIndex(::llvm::Value * index) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Index), index);
setArgOperand(ArgumentIndex::Value::Index, index);
}
::llvm::Value *ExtractElementOp::getResult() {return this;}

Expand Down Expand Up @@ -824,11 +824,11 @@ source


::llvm::Value * FromFixedVectorOp::getSource() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Source));
return getArgOperand(ArgumentIndex::Value::Source);
}

void FromFixedVectorOp::setSource(::llvm::Value * source) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Source), source);
setArgOperand(ArgumentIndex::Value::Source, source);
}
::llvm::Value *FromFixedVectorOp::getResult() {return this;}

Expand Down Expand Up @@ -984,11 +984,11 @@ source


::llvm::Value * IExtOp::getSource() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Source));
return getArgOperand(ArgumentIndex::Value::Source);
}

void IExtOp::setSource(::llvm::Value * source) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Source), source);
setArgOperand(ArgumentIndex::Value::Source, source);
}
::llvm::Value *IExtOp::getResult() {return this;}

Expand Down Expand Up @@ -1082,11 +1082,11 @@ source


::llvm::Value * ITruncOp::getSource() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Source));
return getArgOperand(ArgumentIndex::Value::Source);
}

void ITruncOp::setSource(::llvm::Value * source) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Source), source);
setArgOperand(ArgumentIndex::Value::Source, source);
}
::llvm::Value *ITruncOp::getResult() {return this;}

Expand Down Expand Up @@ -1156,7 +1156,7 @@ source


bool ImmutableOp::getVal() const {
return ::llvm::cast<::llvm::ConstantInt>(getArgOperand(static_cast<uint32_t>(ArgumentIndex::Val)))->getZExtValue() ;
return ::llvm::cast<::llvm::ConstantInt>(getArgOperand(ArgumentIndex::Value::Val))->getZExtValue() ;
}


Expand Down Expand Up @@ -1259,25 +1259,25 @@ index


::llvm::Value * InsertElementOp::getVector() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Vector));
return getArgOperand(ArgumentIndex::Value::Vector);
}

void InsertElementOp::setVector(::llvm::Value * vector) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Vector), vector);
setArgOperand(ArgumentIndex::Value::Vector, vector);
}
::llvm::Value * InsertElementOp::getValue() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Value));
return getArgOperand(ArgumentIndex::Value::Value);
}

void InsertElementOp::setValue(::llvm::Value * value) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Value), value);
setArgOperand(ArgumentIndex::Value::Value, value);
}
::llvm::Value * InsertElementOp::getIndex() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Index));
return getArgOperand(ArgumentIndex::Value::Index);
}

void InsertElementOp::setIndex(::llvm::Value * index) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Index), index);
setArgOperand(ArgumentIndex::Value::Index, index);
}
::llvm::Value *InsertElementOp::getResult() {return this;}

Expand Down Expand Up @@ -1350,18 +1350,18 @@ instName_0


::llvm::Value * InstNameConflictDoubleOp::getInstName() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::InstName));
return getArgOperand(ArgumentIndex::Value::InstName);
}

void InstNameConflictDoubleOp::setInstName(::llvm::Value * instName) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::InstName), instName);
setArgOperand(ArgumentIndex::Value::InstName, instName);
}
::llvm::Value * InstNameConflictDoubleOp::getInstName_0() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::InstName_0));
return getArgOperand(ArgumentIndex::Value::InstName_0);
}

void InstNameConflictDoubleOp::setInstName_0(::llvm::Value * instName_0) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::InstName_0), instName_0);
setArgOperand(ArgumentIndex::Value::InstName_0, instName_0);
}
::llvm::Value *InstNameConflictDoubleOp::getResult() {return this;}

Expand Down Expand Up @@ -1431,11 +1431,11 @@ instName


::llvm::Value * InstNameConflictOp::getInstName() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::InstName));
return getArgOperand(ArgumentIndex::Value::InstName);
}

void InstNameConflictOp::setInstName(::llvm::Value * instName) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::InstName), instName);
setArgOperand(ArgumentIndex::Value::InstName, instName);
}
::llvm::Value *InstNameConflictOp::getResult() {return this;}

Expand Down Expand Up @@ -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<uint32_t>(ArgumentIndex::InstName_0Start)),
value_op_iterator(arg_begin() + ArgumentIndex::Value::InstName_0Start),
value_op_iterator(arg_end()));
}

InstNameConflictVarargsOp *InstNameConflictVarargsOp::replaceInstName_0(::llvm::ArrayRef<Value *> instName_0) {
::llvm::SmallVector<Value *> newArgs;
if (static_cast<uint32_t>(ArgumentIndex::InstName_0Start) > 0)
newArgs.append(arg_begin(), arg_begin() + static_cast<uint32_t>(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<InstNameConflictVarargsOp>(::llvm::CallInst::Create(getCalledFunction(), newArgs, this->getName(), this->getIterator()));
newOp->copyMetadata(*this);
Expand Down Expand Up @@ -1687,11 +1687,11 @@ data


::llvm::Value * SetWriteOp::getData() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Data));
return getArgOperand(ArgumentIndex::Value::Data);
}

void SetWriteOp::setData(::llvm::Value * data) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Data), data);
setArgOperand(ArgumentIndex::Value::Data, data);
}


Expand Down Expand Up @@ -1760,11 +1760,11 @@ data


::llvm::Type * SizeOfOp::getSizeofType() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::SizeofType))->getType();
return getArgOperand(ArgumentIndex::Value::SizeofType)->getType();
}

void SizeOfOp::setSizeofType(::llvm::Type * sizeof_type) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::SizeofType), llvm::PoisonValue::get(sizeof_type));
setArgOperand(ArgumentIndex::Value::SizeofType, llvm::PoisonValue::get(sizeof_type));
}
::llvm::Value *SizeOfOp::getResult() {return this;}

Expand Down Expand Up @@ -2110,7 +2110,7 @@ initial


::llvm::StringRef StringAttrOp::getVal() const {
return ::llvm::cast<::llvm::ConstantDataArray>(::llvm::cast<::llvm::GlobalVariable>(getArgOperand(static_cast<uint32_t>(ArgumentIndex::Val)))->getInitializer())->getAsString() ;
return ::llvm::cast<::llvm::ConstantDataArray>(::llvm::cast<::llvm::GlobalVariable>(getArgOperand(ArgumentIndex::Value::Val))->getInitializer())->getAsString() ;
}


Expand Down Expand Up @@ -2169,11 +2169,11 @@ data


::llvm::Value * WriteOp::getData() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Data));
return getArgOperand(ArgumentIndex::Value::Data);
}

void WriteOp::setData(::llvm::Value * data) {
setArgOperand(static_cast<uint32_t>(ArgumentIndex::Data), data);
setArgOperand(ArgumentIndex::Value::Data, data);
}


Expand Down Expand Up @@ -2234,22 +2234,22 @@ data


::llvm::Value * WriteVarArgOp::getData() const {
return getArgOperand(static_cast<uint32_t>(ArgumentIndex::Data));
return getArgOperand(ArgumentIndex::Value::Data);
}

void WriteVarArgOp::setData(::llvm::Value * data) {
setArgOperand(static_cast<uint32_t>(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<uint32_t>(ArgumentIndex::ArgsStart)),
value_op_iterator(arg_begin() + ArgumentIndex::Value::ArgsStart),
value_op_iterator(arg_end()));
}

WriteVarArgOp *WriteVarArgOp::replaceArgs(::llvm::ArrayRef<Value *> args) {
::llvm::SmallVector<Value *> newArgs;
if (static_cast<uint32_t>(ArgumentIndex::ArgsStart) > 0)
newArgs.append(arg_begin(), arg_begin() + static_cast<uint32_t>(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<WriteVarArgOp>(::llvm::CallInst::Create(getCalledFunction(), newArgs, this->getName(), this->getIterator()));
newOp->copyMetadata(*this);
Expand Down
Loading

0 comments on commit 498d83d

Please sign in to comment.