Skip to content

Commit 5b05667

Browse files
committed
Use cl::opt<std::optional<bool>> for preserve-{bc,ll}-uselistorder options
1 parent dadb949 commit 5b05667

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

llvm/include/llvm/IR/UseListOrder.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_IR_USELISTORDER_H
1515
#define LLVM_IR_USELISTORDER_H
1616

17+
#include "llvm/Support/CommandLine.h"
1718
#include <cstddef>
1819
#include <vector>
1920

@@ -38,6 +39,27 @@ struct UseListOrder {
3839

3940
using UseListOrderStack = std::vector<UseListOrder>;
4041

42+
class PreserveUseListOrderOptionParser
43+
: public cl::parser<std::optional<bool>> {
44+
public:
45+
PreserveUseListOrderOptionParser(cl::Option &O)
46+
: cl::parser<std::optional<bool>>(O) {}
47+
48+
bool parse(cl::Option &O, StringRef ArgName, StringRef Arg,
49+
std::optional<bool> &V) {
50+
if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
51+
Arg == "1") {
52+
V = true;
53+
return false;
54+
}
55+
if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
56+
V = false;
57+
return false;
58+
}
59+
return O.error("Invalid argument '" + Arg + "', Try 0 or 1");
60+
}
61+
};
62+
4163
} // end namespace llvm
4264

4365
#endif // LLVM_IR_USELISTORDER_H

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,12 @@ static cl::opt<bool>
118118
#endif
119119
cl::desc(""));
120120

121-
static cl::opt<bool> PreserveBitcodeUseListOrder(
122-
"preserve-bc-uselistorder",
123-
cl::desc("Preserve use-list order when writing LLVM bitcode."),
124-
cl::init(false), cl::Hidden);
121+
static cl::opt<std::optional<bool>, /*ExternalStorage=*/false,
122+
PreserveUseListOrderOptionParser>
123+
PreserveBitcodeUseListOrder(
124+
"preserve-bc-uselistorder",
125+
cl::desc("Preserve use-list order when writing LLVM bitcode."),
126+
cl::init(std::nullopt), cl::Hidden, cl::ValueOptional);
125127

126128
namespace llvm {
127129
extern FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold;
@@ -222,7 +224,7 @@ class ModuleBitcodeWriterBase : public BitcodeWriterBase {
222224
bool ShouldPreserveUseListOrder,
223225
const ModuleSummaryIndex *Index)
224226
: BitcodeWriterBase(Stream, StrtabBuilder), M(M),
225-
VE(M, ShouldPreserveUseListOrder || PreserveBitcodeUseListOrder),
227+
VE(M, PreserveBitcodeUseListOrder.value_or(ShouldPreserveUseListOrder)),
226228
Index(Index) {
227229
// Assign ValueIds to any callee values in the index that came from
228230
// indirect call profiles and were recorded as a GUID not a Value*

llvm/lib/IR/AsmWriter.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "llvm/IR/TypeFinder.h"
6464
#include "llvm/IR/TypedPointerType.h"
6565
#include "llvm/IR/Use.h"
66+
#include "llvm/IR/UseListOrder.h"
6667
#include "llvm/IR/User.h"
6768
#include "llvm/IR/Value.h"
6869
#include "llvm/Support/AtomicOrdering.h"
@@ -102,10 +103,12 @@ static cl::opt<bool> PrintProfData(
102103
"print-prof-data", cl::Hidden,
103104
cl::desc("Pretty print perf data (branch weights, etc) when dumping"));
104105

105-
static cl::opt<bool> PreserveAssemblyUseListOrder(
106-
"preserve-ll-uselistorder",
107-
cl::desc("Preserve use-list order when writing LLVM assembly."),
108-
cl::init(false), cl::Hidden);
106+
static cl::opt<std::optional<bool>, /*ExternalStorage=*/false,
107+
PreserveUseListOrderOptionParser>
108+
PreserveAssemblyUseListOrder(
109+
"preserve-ll-uselistorder",
110+
cl::desc("Preserve use-list order when writing LLVM assembly."),
111+
cl::init(std::nullopt), cl::Hidden, cl::ValueOptional);
109112

110113
// Make virtual table appear in this compilation unit.
111114
AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
@@ -2977,8 +2980,8 @@ AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
29772980
bool IsForDebug, bool ShouldPreserveUseListOrder)
29782981
: Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
29792982
IsForDebug(IsForDebug),
2980-
ShouldPreserveUseListOrder(ShouldPreserveUseListOrder ||
2981-
PreserveAssemblyUseListOrder) {
2983+
ShouldPreserveUseListOrder(
2984+
PreserveAssemblyUseListOrder.value_or(ShouldPreserveUseListOrder)) {
29822985
if (!TheModule)
29832986
return;
29842987
for (const GlobalObject &GO : TheModule->global_objects())
@@ -2990,7 +2993,8 @@ AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
29902993
const ModuleSummaryIndex *Index, bool IsForDebug)
29912994
: Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
29922995
IsForDebug(IsForDebug),
2993-
ShouldPreserveUseListOrder(PreserveAssemblyUseListOrder) {}
2996+
ShouldPreserveUseListOrder(PreserveAssemblyUseListOrder.value_or(false)) {
2997+
}
29942998

29952999
void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
29963000
if (!Operand) {

0 commit comments

Comments
 (0)