This is an archive of the discontinued LLVM Phabricator instance.

[CommandLine] Keep option default value unset if no cl::init() is used
ClosedPublic

Authored by yrouban on Nov 26 2021, 8:12 AM.

Details

Summary

Current declaration of cl::opt is incoherent between class and non-class specializations of the opt_storage template. There is an inconsistency in the initialization of the Default field: for inClass instances the default constructor is used - it sets the Optional Default field to None; though for non-inClass instances the Default field is set to the type's default value. For non-inClass instances it is impossible to know if the option is defined with cl::init() initializer or not:

cl::opt<int> i1("option-i1");
cl::opt<int> i2("option-i2", cl::init(0));
cl::opt<std::string> s1("option-s1");
cl::opt<std::string> s2("option-s2", cl::init(""));

assert(s1.Default.hasValue() != s2.Default.hasValue()); // Ok
assert(i1.Default.hasValue() != i2.Default.hasValue()); // Fails

This patch changes constructor of the non-class specializations to keep the Default field unset (that is None) rather than initialize it with DataType().

Diff Detail

Event Timeline

yrouban created this revision.Nov 26 2021, 8:12 AM
yrouban requested review of this revision.Nov 26 2021, 8:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 26 2021, 8:12 AM

Any thought? ping

Thanks for looking into this. I'll take a look this afternoon.

Btw, your summary outlines the issue, but doesn't seem to indicate the fix -- could you add that, and perhaps comments where appropriate?

yrouban edited the summary of this revision. (Show Details)Dec 7 2021, 7:24 AM
yrouban added inline comments.
llvm/include/llvm/Support/CommandLine.h
1369

do you want me to add a comment?

// Do not initialize Default. Keep it None.
hintonda added inline comments.Dec 7 2021, 8:16 PM
llvm/include/llvm/Support/CommandLine.h
1369

This change was explicitly made in 2013, so we need to be careful not to break anything that depends on it.

llvm/unittests/Support/CommandLineTest.cpp
1825

Are these additional cl::init calls needed?

yrouban added inline comments.Dec 7 2021, 10:07 PM
llvm/include/llvm/Support/CommandLine.h
1369

Thanks for pointing this out. That change looks to have a rationale limited to one particular option, which has been changed since then and got explicit cl::init value in LoopVectorize.cpp.

llvm/unittests/Support/CommandLineTest.cpp
1825

Yes, I found them needed as the test failed otherwise.

For example, the first cl::init(false) for the option "a" when reset by cl::ResetAllOptionOccurrences() ...

CommandLine.h:

template <class T,
          class = std::enable_if_t<std::is_assignable<T &, T>::value>>
void setDefaultImpl() {
  const OptionValue<DataType> &V = this->getDefault();
  if (V.hasValue())
    this->setValue(V.getValue());
}

Without the explicit default value specified V.hasValue() is None and setDefaultValueImple<bool>() does not call setValue(). So the option is left not reset at line 1843.

1874

Without explicit cl::init(false) for the option "a" it is left not reset at this here.

hintonda added inline comments.Dec 8 2021, 3:29 PM
llvm/unittests/Support/CommandLineTest.cpp
1825

Ah, got it.

Would it be possible to tweak cl::ResetAllOptionOccurrences() instead? That way users don't have to modify their code.

I'm just worried about breaking code that's been around for a while.

yrouban added inline comments.Dec 9 2021, 1:21 AM
llvm/unittests/Support/CommandLineTest.cpp
1825

Logically, what is setDefaultImpl() supposed to do? reset the option to its default value (that is ValueType()) or reset it to the Default value (that is what was specified with cl::init())? I prepared a separate patch D115433 that resets to the Default if it is defined otherwise the option is reset to ValueType(). So these cl::init()s are not needed.

yrouban updated this revision to Diff 393068.Dec 9 2021, 1:25 AM

Extracted a separate change D115433.

ping, please review.

ping, please review.

lattner accepted this revision.Feb 24 2022, 8:39 PM

LGTM, thank you for improving this!

This revision is now accepted and ready to land.Feb 24 2022, 8:39 PM

LGTM, thank you for improving this!

Thank you very much. Consider reviewing the related
D115433 [CommandLine] Reset option to its default if its Default field is undefined

This revision was landed with ongoing or failed builds.Mar 10 2022, 11:25 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptMar 10 2022, 11:25 PM