Skip to content

Commit 4f5f43e

Browse files
authored
Fix ASAN initialization-order-fiasco issue in tensor_layout_print.mlir test (#8117)
IIUC, the initialization order between static and non-static variables is not guaranteed, so we can't use the previous non-static variable to initialize a static one later on. Working around that by moving it into a static function variable. We discovered this when upgrading to a newer LLVM version, so it might only be reproducible with new LLVM. Here is the error: ``` ==3551==ERROR: AddressSanitizer: initialization-order-fiasco on address 0x557bc517caa0 at pc 0x557bc3f2fbb2 bp 0x7ffda74ef270 sp 0x7ffda74ef268 READ of size 8 at 0x557bc517caa0 thread T0 #0 0x557bc3f2fbb1 in getName llvm/include/llvm/Support/CommandLine.h:194:38 #1 0x557bc3f2fbb1 in operator() llvm/lib/Support/CommandLine.cpp:347:5 #2 0x557bc3f2fbb1 in __invoke<(lambda at llvm/lib/Support/CommandLine.cpp:347:5) &, llvm::cl::OptionCategory *> libcxx/include/__type_traits/invoke.h:87:27 #3 0x557bc3f2fbb1 in __count_if<std::__u::_ClassicAlgPolicy, llvm::SmallPtrSetIterator<llvm::cl::OptionCategory *>, llvm::SmallPtrSetIterator<llvm::cl::OptionCategory *>, std::__u::__identity, (lambda at llvm/lib/Support/CommandLine.cpp:347:5)> libcxx/include/__algorithm/count_if.h:30:9 #4 0x557bc3f2fbb1 in count_if<llvm::SmallPtrSetIterator<llvm::cl::OptionCategory *>, (lambda at llvm/lib/Support/CommandLine.cpp:347:5)> libcxx/include/__algorithm/count_if.h:41:10 #5 0x557bc3f2fbb1 in count_if<llvm::SmallPtrSet<llvm::cl::OptionCategory *, 16U> &, (lambda at llvm/lib/Support/CommandLine.cpp:347:5)> llvm/include/llvm/ADT/STLExtras.h:1981:10 #6 0x557bc3f2fbb1 in registerCategory llvm/lib/Support/CommandLine.cpp:347:5 #7 0x557bc3f2fbb1 in llvm::cl::OptionCategory::registerCategory() llvm/lib/Support/CommandLine.cpp:484:17 #8 0x557bc4504950 in OptionCategory llvm/include/llvm/Support/CommandLine.h:191:5 #9 0x557bc4504950 in __cxx_global_var_init llvm/lib/CodeGen/GlobalISel/Combiner.cpp:37:20 ```
1 parent e1380d7 commit 4f5f43e

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

bin/triton-tensor-layout.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,44 @@ using namespace mlir;
3939
// CLI options
4040
//===--------------------------------------------------------------------===//
4141

42-
cl::OptionCategory PrinterCategory("Available Print Options",
43-
"Options for the tensor layout printing.");
42+
static cl::OptionCategory &getPrinterCategory() {
43+
static cl::OptionCategory PrinterCategory(
44+
"Available Print Options", "Options for the tensor layout printing.");
45+
return PrinterCategory;
46+
}
4447

4548
static cl::opt<std::string> InputFile(
4649
"i", cl::desc("File that contains the tensor data layout attributes"),
47-
cl::init(""), cl::value_desc("filename"), cl::cat(PrinterCategory));
50+
cl::init(""), cl::value_desc("filename"), cl::cat(getPrinterCategory()));
4851

4952
static cl::opt<std::string>
5053
OutputFile("o", cl::desc("Output file to write the layout into"),
5154
cl::init(""), cl::value_desc("filename"),
52-
cl::cat(PrinterCategory));
55+
cl::cat(getPrinterCategory()));
5356

5457
static cl::opt<std::string>
5558
DataLayoutStr("l", cl::desc("Tensor data layout attribute in string"),
5659
cl::value_desc("layout-string"), cl::init(""),
57-
cl::cat(PrinterCategory));
60+
cl::cat(getPrinterCategory()));
5861

5962
static cl::list<std::string>
6063
AliasName("alias-names",
6164
cl::desc("A list of alias names (separated by comma) of the "
6265
"layout attributes in the input file"),
6366
cl::value_desc("name1,name2,name3,..."), cl::CommaSeparated,
64-
cl::ZeroOrMore, cl::cat(PrinterCategory));
67+
cl::ZeroOrMore, cl::cat(getPrinterCategory()));
6568

6669
static cl::opt<bool> UseHWPointOfView(
6770
"use-hw-view",
6871
llvm::cl::desc(
6972
"Print the layout in hardware point of view. This means the output is "
7073
"from the warp's perspective. Otherwise, the output is from the "
7174
"tensor's perspective (e.g., each element maps to xxx thread)."),
72-
cl::init(false), cl::cat(PrinterCategory));
75+
cl::init(false), cl::cat(getPrinterCategory()));
7376

7477
static cl::opt<std::string> TensorStr(
7578
"t", cl::desc("Tensor shape and element type (e.g., tensor<2x2xf32>)"),
76-
cl::init(""), cl::value_desc("tensor-type"), cl::cat(PrinterCategory));
79+
cl::init(""), cl::value_desc("tensor-type"), cl::cat(getPrinterCategory()));
7780

7881
//===--------------------------------------------------------------------===//
7982
// Helper functions
@@ -180,7 +183,7 @@ static LogicalResult printLayoutFromString(MLIRContext *context,
180183
//===--------------------------------------------------------------------===//
181184

182185
int main(int argc, char **argv) {
183-
cl::HideUnrelatedOptions(PrinterCategory);
186+
cl::HideUnrelatedOptions(getPrinterCategory());
184187
cl::ParseCommandLineOptions(argc, argv, "tensor layout printer\n");
185188

186189
DialectRegistry registry;

0 commit comments

Comments
 (0)