internal error: entered unreachable code: The given type is not a basic type. (when looking at the operands of a call)
Describe the Bug I'm trying to use inkwell to inspect some LLVM IR.
To Reproduce
use std::path::Path;
use inkwell::{context::Context, memory_buffer::MemoryBuffer, values::InstructionOpcode};
fn main() {
// Read the IR file into a memory buffer
let memory_buffer = MemoryBuffer::create_from_file(Path::new("/tmp/test.ll")).unwrap();
let context = Context::create();
// Parse the IR into a module
let module = context
.create_module_from_ir(memory_buffer).unwrap();
let main = module.get_function("main").unwrap();
for bb in main.get_basic_blocks() {
for instruction in bb.get_instructions() {
eprintln!("Instruction: {:?}", instruction);
if instruction.get_opcode() == InstructionOpcode::Call {
eprintln!("Looking at {} operands", instruction.get_num_operands());
for (i, operand) in instruction.get_operands().enumerate() {
eprintln!("Operand {i}: {:?}", operand);
}
}
}
}
}
I have applied this to the LLVM IR for the following file:
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("/tmp/test.txt", O_RDONLY);
if (fd >= 0) {
char buffer[256];
read(fd, buffer, sizeof(buffer));
close(fd);
}
return 0;
}
In the first call, this panics with
internal error: entered unreachable code: The given type is not a basic type.
Expected Behavior
I expected to be able to look at all the operands of the call.
LLVM Version (please complete the following information):
- LLVM Version: [e.g. 6.0] llvm18-1
- Inkwell Branch Used: 0.7.1
Desktop (please complete the following information):
- OS: [e.g. Ubuntu 16.04, Windows 10] macOS 12.7.6.
Looking at it more in depth, the error shows up at the following IR:
call void @llvm.dbg.declare(metadata i32* %2, metadata !16, metadata !DIExpression()), !dbg !17
the panic is raised because the arguments are metadata, which cannot be converted into BasicValueEnum. If I skip llvm.dbg.*, other functions work nicely.
I think we have a MetadataBasicValueEnum which is probably want wherever it's failing
Let me see if I can write a PR for this.
PR opened.