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>
Tim Northover via llvm-dev
2019-Feb-22 18:08 UTC
[llvm-dev] Create the GlobalVariable which have extern in one header file
On Fri, 22 Feb 2019 at 04:15, Mustakimur Khandaker <mrk15e at my.fsu.edu> wrote:> Why it is creating another global variable instead of linking?You're explicitly calling "new" which creates a global variable. If there's an existing global you want to reuse you should call Module::getOrInsertGlobal instead. But...> Is that because of array size mismatch?It's definitely not helping, and if you can't come up with a way to get the type when creating the original variable you might have to create two variables after all. In that case you'd create your new variable with a known array length, use ConstantExpr::getBitCast to cast it to [0 x i8*]* and then call replaceAllUsesWith to make the existing code use your new variable with initializer. Cheers. Tim.