ApsarasX/llvm-bindings

[Bug] CreateGEP causes segmentation fault

raphtlw opened this issue · 2 comments

Your environment

  • OS version: macOS Monterey 12.1
  • Node.js version: 17.1.0
  • LLVM version: 13.0.1

Describe the bug

I'm trying to create a getelementptr statement using the bindings, but I get a segmentation fault when I run the program.

const printConst = builder.CreateGlobalString(
  printArg.text,
  `${VARIABLE_STRING_PREFIX}.${VARIABLE_COUNT_GLOBAL}`,
  0,
  module
);
builder.CreateGEP(printConst.getType(), printConst, [
  builder.getInt64(0),
  builder.getInt64(0),
]);

Expected behavior

It should produce a statement similar to this:

%cast210 = getelementptr [13 x i8],[13 x i8]* @.str, i64 0, i64 0

It's not a problem with llvm-bindings, but you are using LLVM incorrectly.

The correct usage should be like the following.

const printConst = builder.CreateGlobalString(
    'string content',
    '.str',
    0,
    module
);
const ptr = builder.CreateGEP(printConst.getValueType(), printConst, [
    builder.getInt64(0),
    builder.getInt64(0),
]);

Note: Please install llvm-bindings v0.3.6 to use printConst.getValueType() method.


Of couse, the above code has the same effect as the following code.

const printConst = builder.CreateGlobalStringPtr(
    'string content',
    '.str',
    0,
    module
);

The type of printConst in this case is different from the previous case, please refer to LLVM IRBuilder.h#L1845-L1849 for more details.

@ApsarasX Thanks for the detailed reply! I feel so dumb haha, I'd write in a pull request that would document some of the functions and provide some usage examples.