Hone logo
Hone
Problems

LLVM IR Inspection Tool in Rust

This challenge tasks you with building a simple tool in Rust that can parse and inspect LLVM Intermediate Representation (IR). LLVM IR is a low-level assembly-like language used by the LLVM compiler infrastructure. This tool will allow you to extract information from LLVM IR files, providing a foundation for more complex analysis and transformation tasks.

Problem Description

You are to create a Rust program that takes an LLVM IR file as input and prints the names of all functions defined within that file. The program should parse the LLVM IR, identify function declarations, and extract their names. The program should handle basic LLVM IR syntax and gracefully handle invalid or malformed IR files.

Key Requirements:

  • Parsing: The program must be able to parse a valid LLVM IR file.
  • Function Identification: The program must correctly identify function declarations within the IR.
  • Name Extraction: The program must extract the names of the identified functions.
  • Error Handling: The program should handle invalid or malformed IR files gracefully, printing an error message to standard error and exiting with a non-zero exit code.
  • Command-Line Argument: The program should accept the LLVM IR file path as a command-line argument.

Expected Behavior:

When given a valid LLVM IR file as input, the program should print a list of function names, one per line, to standard output. If the input file is invalid or cannot be parsed, the program should print an error message to standard error and exit with a non-zero exit code.

Edge Cases to Consider:

  • Empty File: Handle the case where the input file is empty.
  • Malformed IR: Handle cases where the IR is syntactically incorrect.
  • Comments: Ignore comments within the IR file.
  • Metadata: Ignore metadata sections.
  • Functions with Attributes: The function name extraction should work regardless of any attributes associated with the function.
  • Nested Functions (if present): Focus on top-level function definitions. Nested functions are outside the scope of this challenge.

Examples

Example 1:

Input: test.ll (containing: "define i32 @foo() { ... }" and "define i32 @bar() { ... }")
Output:
foo
bar
Explanation: The program successfully parsed the IR and extracted the names "foo" and "bar".

Example 2:

Input: invalid.ll (containing: "define i32 @foo() { invalid syntax }")
Output:
Error: Could not parse LLVM IR.
Explanation: The program detected invalid syntax and printed an error message to stderr. The exit code will be non-zero.

Example 3:

Input: empty.ll (an empty file)
Output:
Explanation: The program handles the empty file gracefully, producing no output (or a minimal error message if the parser detects an empty file).

Constraints

  • Input File Size: The input LLVM IR file should be no larger than 1MB.
  • Parsing Library: You are encouraged to use the llvm-sys crate for parsing LLVM IR. You may also use other crates, but llvm-sys is the recommended starting point.
  • Performance: The program should parse and process the IR file within a reasonable time (e.g., less than 1 second for a 1MB file). Optimization is not the primary focus, but excessive inefficiency should be avoided.
  • Error Messages: Error messages should be informative and helpful for debugging.
  • Rust Version: Use Rust 1.65 or later.

Notes

  • The llvm-sys crate provides low-level bindings to the LLVM library. You will need to link against the LLVM library when compiling your program. Refer to the llvm-sys crate documentation for details on linking.
  • Start by parsing the IR file and creating an LLVM Module object.
  • Iterate through the functions within the module and extract their names.
  • Consider using error handling mechanisms like Result to handle potential parsing errors.
  • Focus on the core functionality of function name extraction. More advanced IR analysis is beyond the scope of this challenge.
  • The LLVM IR format can be complex. This challenge focuses on a subset of the format – function declarations.
Loading editor...
rust