哪些情况会导致 LLVM IR 中“指令的使用不是指令”?

Tan*_*nin 1 llvm

我多次收到此错误。然后,当我改变周围的事情时,它就起作用了。我的代码太复杂,无法在这里发布。我无法简化和重现问题。

LLVM中引发异常的源代码在这里:http ://llvm.org/doxygen/Verifier_8cpp_source.html :

   // Check that all uses of the instruction, if they are instructions
   // themselves, actually have parent basic blocks.  If the use is not an
   // instruction, it is an error!
   for (Use &U : I.uses()) {
     if (Instruction *Used = dyn_cast<Instruction>(U.getUser()))
       Assert(Used->getParent() != nullptr,
              "Instruction referencing"
              " instruction not embedded in a basic block!",
              &I, Used);
     else {
       CheckFailed("Use of instruction is not an instruction!", U);
       return;
     }
   }
Run Code Online (Sandbox Code Playgroud)

但我还是不明白什么意思

所以,我想知道是否有人有一个小例子导致“使用指令不是指令”错误,并解释为什么会发生。

谢谢你!

arr*_*owd 7

您可能在代码中的某处生成了伪造的 IR。例如,您提到的断言可能是通过构造一个使用其他指令的结果作为操作数的常量表达式来触发的:

%0 = <some instruction producing result>
store i8* getelementptr inbounds ([123 x i8], [123 x i8]* @some_string, i32 %0, i32 0), ...
Run Code Online (Sandbox Code Playgroud)

这里,该getelementptr inbounds ...部分是一个常量表达式,除了其他常量之外不能包含任何内容。因此,我们不能%0在这里用作索引。

相反,我们需要使用getelementptr 指令

%0 = <some instruction producing result>
%1 = i8* getelementptr inbounds ([123 x i8], [123 x i8]* @some_string, i32 %0, i32 0)
store i8* %1, ...
Run Code Online (Sandbox Code Playgroud)

至于您的情况,您可以I.dump()从代码或内部调试器中调用来准确找出是什么指令导致断言失败。