Mustakimur Khandaker via llvm-dev
2019-Feb-22 02:53 UTC
[llvm-dev] Create the GlobalVariable which have extern in one header file
Hi Good day. I am facing issue with creating a GlobalVariable. I already have an extern for that global in a header file to use it in the following way extern const void* DATA_TABLE[]; And with a LLVM PASS, I am trying to create this array with the same name and with initializer. So, I have following: GlobalVariable *gvar_data = new GlobalVariable( M, blockItems->getType(), true, GlobalValue::CommonLinkage, blockItems, "DATA_TABLE"); gvar_data->setAlignment(16); gvar_data->setSection("data_section"); gvar_data->addAttribute(llvm::Attribute::OptimizeNone); I am not sure if I am using the correct Linkage or not. The pass has failed to complete it. Here is the runtime fault. Any guess what I am doing incorrect? 'common' global must have a zero initializer! [10 x i8*]* @DATA_TABLE.1 LLVM ERROR: Broken module found, compilation aborted:::::::::! -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190222/4fd50559/attachment.html>
Tim Northover via llvm-dev
2019-Feb-22 05:02 UTC
[llvm-dev] Create the GlobalVariable which have extern in one header file
On Thu, 21 Feb 2019 at 18:42, Mustakimur Khandaker via llvm-dev <llvm-dev at lists.llvm.org> wrote:> GlobalVariable *gvar_data = new GlobalVariable( > M, blockItems->getType(), true, GlobalValue::CommonLinkage, > blockItems, "DATA_TABLE"); > gvar_data->setAlignment(16); > gvar_data->setSection("data_section"); > gvar_data->addAttribute(llvm::Attribute::OptimizeNone); > > I am not sure if I am using the correct Linkage or not. > The pass has failed to complete it. Here is the runtime fault. Any guess what I am doing incorrect?As you've guessed, it's the linkage. "common" is the name for the C situation where multiple source files simply declare "int a;" with nothing else specified. The names get merged, and the variable gets zero-initialized by default. A "real" definition (like you're trying to provide) would take precedence over this and specify the actual data. You almost certainly want GlobalValue::ExternalLinkage for a bog-standard global variable. Cheers. Tim.
Mustakimur Khandaker via llvm-dev
2019-Feb-22 14:15 UTC
[llvm-dev] Create the GlobalVariable which have extern in one header file
I have changed it to ExternalLinkage. Now, in LLVM IR, it looks like that: @DATA_TABLE = external global [0 x i8*], section "data_section", align 16 #0 @DATA_TABLE.1 = global [10 x i8*] [i8* inttoptr (i64 53415718 to i8*), i8* bitcast (void (%class.Hello*)* @_ZN5Hello5ptofnEv to i8*),...], section "data_section", align 16 #0 In my uses: %arrayidx = getelementptr inbounds [0 x i8*], [0 x i8*]* @DATA_TABLE, i64 0, i64 %3 Why it is creating another global variable instead of linking? Is that because of array size mismatch? Here is my code: GlobalVariable *old = M.getGlobalVariable("DATA_TABLE"); old->setAlignment(16); old->setSection("data_section"); old->addAttribute(llvm::Attribute::OptimizeNone); old->setDSOLocal(false); GlobalVariable *gNew = new GlobalVariable( M, blockItems->getType(), false, GlobalValue::ExternalLinkage, blockItems, "DATA_TABLE"); gNew->setAlignment(16); gNew->setSection("data_section"); gNew->addAttribute(llvm::Attribute::OptimizeNone); On Feb 22 2019, at 12:02 am, Tim Northover <t.p.northover at gmail.com> wrote: On Thu, 21 Feb 2019 at 18:42, Mustakimur Khandaker via llvm-dev <llvm-dev at lists.llvm.org> wrote: GlobalVariable *gvar_data = new GlobalVariable( M, blockItems->getType(), true, GlobalValue::CommonLinkage, blockItems, "DATA_TABLE"); gvar_data->setAlignment(16); gvar_data->setSection("data_section"); gvar_data->addAttribute(llvm::Attribute::OptimizeNone); I am not sure if I am using the correct Linkage or not. The pass has failed to complete it. Here is the runtime fault. Any guess what I am doing incorrect? As you've guessed, it's the linkage. "common" is the name for the C situation where multiple source files simply declare "int a;" with nothing else specified. The names get merged, and the variable gets zero-initialized by default. A "real" definition (like you're trying to provide) would take precedence over this and specify the actual data. You almost certainly want GlobalValue::ExternalLinkage for a bog-standard global variable. Cheers. Tim. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190222/a413a1b9/attachment-0001.html>