Skip to content

Commit

Permalink
Use ArgumentIndex enum (#114)
Browse files Browse the repository at this point in the history
Instead of inserting the index value, use the enum value from
ArgumentIndex.
  • Loading branch information
tsymalla authored Dec 4, 2024
1 parent b7ee542 commit 87a0bea
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 102 deletions.
24 changes: 10 additions & 14 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 Enum : uint32_t {\n";
for (const auto &[index, argName] : llvm::enumerate(argNames))
out << tgfmt("$0 = $1,\n", &fmt, argName, numSuperclassArgs + index);
out << "};";
out << "};};";
}
}

Expand All @@ -215,17 +215,18 @@ void AccessorBuilder::emitGetterDefinition() const {
std::string fromLlvm;

if (!m_arg.type->isVarArgList()) {
fromLlvm = tgfmt("getArgOperand($index)", &m_fmt);
fromLlvm = tgfmt(
"getArgOperand(ArgumentIndex::$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() + $index),
value_op_iterator(arg_begin() + ArgumentIndex::$Name$0),
value_op_iterator(arg_end())))",
&m_fmt);
&m_fmt, "Start");
}

m_fmt.addSubst("fromLlvm", fromLlvm);
Expand All @@ -251,7 +252,7 @@ void AccessorBuilder::emitSetterDefinition() const {
m_os << tgfmt(R"(
void $_op::set$Name($cppType $name) {
setArgOperand($index, $toLlvm);
setArgOperand(ArgumentIndex::$Name, $toLlvm);
})",
&m_fmt);
}
Expand All @@ -263,32 +264,27 @@ void AccessorBuilder::emitVarArgReplacementDefinition() const {
$_op *$_op::replace$Name(::llvm::ArrayRef<Value *> $name) {
::llvm::SmallVector<Value *> newArgs;
if ($index > 0)
newArgs.append(arg_begin(), arg_begin() + $index);
if (ArgumentIndex::$Name$0 > 0)
newArgs.append(arg_begin(), arg_begin() + ArgumentIndex::$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);
this->replaceAllUsesWith(newOp);
this->eraseFromParent();
return newOp;
})",
&m_fmt);
&m_fmt, "Start");
}

void OperationBase::emitArgumentAccessorDefinitions(llvm::raw_ostream &out,
FmtContext &fmt) const {
unsigned numSuperclassArgs = 0;
if (m_superclass)
numSuperclassArgs = m_superclass->getNumFullArguments();

for (const auto &indexedArg : llvm::enumerate(m_arguments)) {
FmtContextScope scope(fmt);

const NamedValue &arg = indexedArg.value();
AccessorBuilder builder{fmt, out, arg, m_attrTypes[indexedArg.index()]};

fmt.withContext("getContext()");
fmt.addSubst("index", Twine(numSuperclassArgs + indexedArg.index()));
fmt.addSubst("cppType", arg.type->getGetterCppType());
fmt.addSubst("name", arg.name);
fmt.addSubst("Name", convertToCamelFromSnakeCase(arg.name, true));
Expand Down
Loading

0 comments on commit 87a0bea

Please sign in to comment.