Skip to content

Commit

Permalink
[LLVM][Support] Support for llvm::cl::list's default values
Browse files Browse the repository at this point in the history
This patch introduces support for default values of list of CL options.
It fixes the issue in #52667

Reviewed By: bkramer

Differential Revision: https://reviews.llvm.org/D135311
  • Loading branch information
tyb0807 committed Oct 6, 2022
1 parent 7404b85 commit a4deb14
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
63 changes: 61 additions & 2 deletions llvm/include/llvm/Support/CommandLine.h
Expand Up @@ -437,10 +437,22 @@ template <class Ty> struct initializer {
template <class Opt> void apply(Opt &O) const { O.setInitialValue(Init); }
};

template <class Ty> struct list_initializer {
ArrayRef<Ty> Inits;
list_initializer(ArrayRef<Ty> Vals) : Inits(Vals) {}

template <class Opt> void apply(Opt &O) const { O.setInitialValues(Inits); }
};

template <class Ty> initializer<Ty> init(const Ty &Val) {
return initializer<Ty>(Val);
}

template <class Ty>
list_initializer<Ty> list_init(ArrayRef<Ty> Vals) {
return list_initializer<Ty>(Vals);
}

// Allow the user to specify which external variable they want to store the
// results of the command line argument processing into, if they don't want to
// store it in the option itself.
Expand Down Expand Up @@ -1504,6 +1516,9 @@ extern template class opt<bool>;
//
template <class DataType, class StorageClass> class list_storage {
StorageClass *Location = nullptr; // Where to store the object...
std::vector<OptionValue<DataType>> Default =
std::vector<OptionValue<DataType>>();
bool DefaultAssigned = false;

public:
list_storage() = default;
Expand All @@ -1517,12 +1532,22 @@ template <class DataType, class StorageClass> class list_storage {
return false;
}

template <class T> void addValue(const T &V) {
template <class T> void addValue(const T &V, bool initial = false) {
assert(Location != nullptr &&
"cl::location(...) not specified for a command "
"line option with external storage!");
Location->push_back(V);
if (initial)
Default.push_back(V);
}

const std::vector<OptionValue<DataType>> &getDefault() const {
return Default;
}

void assignDefault() { DefaultAssigned = true; }
void overwriteDefault() { DefaultAssigned = false; }
bool isDefaultAssigned() { return DefaultAssigned; }
};

// Define how to hold a class type object, such as a string.
Expand All @@ -1535,6 +1560,8 @@ template <class DataType, class StorageClass> class list_storage {
//
template <class DataType> class list_storage<DataType, bool> {
std::vector<DataType> Storage;
std::vector<OptionValue<DataType>> Default;
bool DefaultAssigned = false;

public:
using iterator = typename std::vector<DataType>::iterator;
Expand Down Expand Up @@ -1598,7 +1625,19 @@ template <class DataType> class list_storage<DataType, bool> {
std::vector<DataType> *operator&() { return &Storage; }
const std::vector<DataType> *operator&() const { return &Storage; }

template <class T> void addValue(const T &V) { Storage.push_back(V); }
template <class T> void addValue(const T &V, bool initial = false) {
Storage.push_back(V);
if (initial)
Default.push_back(OptionValue<DataType>(V));
}

const std::vector<OptionValue<DataType>> &getDefault() const {
return Default;
}

void assignDefault() { DefaultAssigned = true; }
void overwriteDefault() { DefaultAssigned = false; }
bool isDefaultAssigned() { return DefaultAssigned; }
};

//===----------------------------------------------------------------------===//
Expand All @@ -1622,6 +1661,10 @@ class list : public Option, public list_storage<DataType, StorageClass> {
StringRef Arg) override {
typename ParserClass::parser_data_type Val =
typename ParserClass::parser_data_type();
if (list_storage<DataType, StorageClass>::isDefaultAssigned()) {
clear();
list_storage<DataType, StorageClass>::overwriteDefault();
}
if (Parser.parse(*this, ArgName, Arg, Val))
return true; // Parse Error!
list_storage<DataType, StorageClass>::addValue(Val);
Expand All @@ -1647,6 +1690,8 @@ class list : public Option, public list_storage<DataType, StorageClass> {
void setDefault() override {
Positions.clear();
list_storage<DataType, StorageClass>::clear();
for (auto &Val : list_storage<DataType, StorageClass>::getDefault())
list_storage<DataType, StorageClass>::addValue(Val.getValue());
}

void done() {
Expand All @@ -1666,6 +1711,20 @@ class list : public Option, public list_storage<DataType, StorageClass> {
return Positions[optnum];
}

void clear() {
Positions.clear();
list_storage<DataType, StorageClass>::clear();
}

// setInitialValues - Used by the cl::list_init modifier...
void setInitialValues(ArrayRef<DataType> Vs) {
assert(!(list_storage<DataType, StorageClass>::isDefaultAssigned()) &&
"Cannot have two default values");
list_storage<DataType, StorageClass>::assignDefault();
for (auto &Val : Vs)
list_storage<DataType, StorageClass>::addValue(Val, true);
}

void setNumAdditionalVals(unsigned n) { Option::setNumAdditionalVals(n); }

template <class... Mods>
Expand Down
29 changes: 26 additions & 3 deletions llvm/unittests/Support/CommandLineTest.cpp
Expand Up @@ -1038,23 +1038,40 @@ TEST(CommandLineTest, ResponseFileEOLs) {
}
}

TEST(CommandLineTest, SetDefautValue) {
TEST(CommandLineTest, SetDefaultValue) {
cl::ResetCommandLineParser();

StackOption<std::string> Opt1("opt1", cl::init("true"));
StackOption<bool> Opt2("opt2", cl::init(true));
cl::alias Alias("alias", llvm::cl::aliasopt(Opt2));
StackOption<int> Opt3("opt3", cl::init(3));

const char *args[] = {"prog", "-opt1=false", "-opt2", "-opt3"};
llvm::SmallVector<int, 3> IntVals = {1, 2, 3};
llvm::SmallVector<std::string, 3> StrVals = {"foo", "bar", "baz"};

StackOption<int, cl::list<int>> List1(
"list1", cl::list_init<int>(llvm::ArrayRef<int>(IntVals)),
cl::CommaSeparated);
StackOption<std::string, cl::list<std::string>> List2(
"list2", cl::list_init<std::string>(llvm::ArrayRef<std::string>(StrVals)),
cl::CommaSeparated);
cl::alias ListAlias("list-alias", llvm::cl::aliasopt(List2));

const char *args[] = {"prog", "-opt1=false", "-list1", "4",
"-list1", "5,6", "-opt2", "-opt3"};

EXPECT_TRUE(
cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
cl::ParseCommandLineOptions(7, args, StringRef(), &llvm::nulls()));

EXPECT_EQ(Opt1, "false");
EXPECT_TRUE(Opt2);
EXPECT_EQ(Opt3, 3);

for (size_t I = 0, E = IntVals.size(); I < E; ++I) {
EXPECT_EQ(IntVals[I] + 3, List1[I]);
EXPECT_EQ(StrVals[I], List2[I]);
}

Opt2 = false;
Opt3 = 1;

Expand All @@ -1071,7 +1088,13 @@ TEST(CommandLineTest, SetDefautValue) {
EXPECT_EQ(Opt1, "true");
EXPECT_TRUE(Opt2);
EXPECT_EQ(Opt3, 3);
for (size_t I = 0, E = IntVals.size(); I < E; ++I) {
EXPECT_EQ(IntVals[I], List1[I]);
EXPECT_EQ(StrVals[I], List2[I]);
}

Alias.removeArgument();
ListAlias.removeArgument();
}

TEST(CommandLineTest, ReadConfigFile) {
Expand Down
5 changes: 5 additions & 0 deletions mlir/include/mlir/Pass/PassOptions.h
Expand Up @@ -229,6 +229,10 @@ class PassOptions : protected llvm::cl::SubCommand {

bool handleOccurrence(unsigned pos, StringRef argName,
StringRef arg) override {
if (this->isDefaultAssigned()) {
this->clear();
this->overwriteDefault();
}
this->optHasValue = true;
return failed(detail::pass_options::parseCommaSeparatedList(
*this, argName, arg, elementParser,
Expand Down Expand Up @@ -418,6 +422,7 @@ struct OptionValue<mlir::OpPassManager> final : GenericOptionValue {
using WrapperType = mlir::OpPassManager;

OptionValue();
OptionValue(const OptionValue<mlir::OpPassManager> &rhs);
OptionValue(const mlir::OpPassManager &value);
OptionValue<mlir::OpPassManager> &operator=(const mlir::OpPassManager &rhs);
~OptionValue();
Expand Down
5 changes: 5 additions & 0 deletions mlir/lib/Pass/PassRegistry.cpp
Expand Up @@ -348,6 +348,11 @@ llvm::cl::OptionValue<OpPassManager>::OptionValue(
const mlir::OpPassManager &value) {
setValue(value);
}
llvm::cl::OptionValue<OpPassManager>::OptionValue(
const llvm::cl::OptionValue<mlir::OpPassManager> &rhs) {
if (rhs.hasValue())
setValue(rhs.getValue());
}
llvm::cl::OptionValue<OpPassManager> &
llvm::cl::OptionValue<OpPassManager>::operator=(
const mlir::OpPassManager &rhs) {
Expand Down

0 comments on commit a4deb14

Please sign in to comment.