How do I get the target Triple in this LLVM portion?

Specifically in the DataLayout class, how can I get the triple of the target machine?
For example, if I’m in the function DataLayout::reset and I want to do something if the current target is a specific machine. How would I do that?

I tried to trace the DataLayout constructor call to the Module Constructor call here in Module.cpp:

Module::Module(StringRef MID, LLVMContext &C)
    : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>(-1)),
      Materializer(), ModuleID(std::string(MID)),
      SourceFileName(std::string(MID)), DL("") {

But even trying to get the triple from the module via this->getTargetTriple() returns an empty string, I’m guessing because the Module target isn’t set yet.

I found where the module target is being set, in LLParser::parseTargetDefinition, but if I try to set the right datalayout after that, I think there may have been some processing already after it set the datalayout under case lltok::kw_datalayout: or the Module constructor, as what I need to change isn’t happening.

So… how would I go about this?

I think the idea is that you don’t check the target in reset, but use the target to construct the string passed to reset. The sources in clang/lib/Basic/Targets construct data layout strings based on triple features. Sorry if that doesn’t help for the particular thing you are trying to do.

1 Like

I tried to do this, but even if I alter the string passed into reset to include the target. This is parsed in LLParser::parseTargetDefinition where the datalayout for the Module is set.

Prior to this, this module constructor already creates the DataLayout in

Module::Module(StringRef MID, LLVMContext &C)
    : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>(-1)),
      Materializer(), ModuleID(std::string(MID)),
      SourceFileName(std::string(MID)), DL("") {
  Context.addModule(this);
}

Here setPointerAlignmentInBits is set, and machine code is generated from this pointer alignements. Then when I call my custom reset function in LLParser::parseTargetDefinition after getting the target, the machine code generated still uses the original setPointerAlignmentInBits call, not my custom one?