Convert
convert
¶
Exporting LLVM IR.
The convert_module() function converts an xDSL module containing LLVM dialect
operations to an llvmlite.ir.Module. The same conversion is available through
the CLI command xdsl-opt -t llvm.
For example, take the following hello.mlir file:
builtin.module {
llvm.mlir.global internal constant @hello_str("Hello, World!\0A\00") {addr_space = 0 : i32} : !llvm.array<15 x i8>
llvm.func @puts(!llvm.ptr)
llvm.func @main() -> i32 {
%0 = llvm.mlir.addressof @hello_str : !llvm.ptr
llvm.call @puts(%0) : (!llvm.ptr) -> ()
%ret = llvm.mlir.constant(0 : i32) : i32
llvm.return %ret : i32
}
}
The same conversion is available from the Python API, with the following hello.py
file:
# /// script
# requires-python = ">=3.11"
# dependencies = ["xdsl[llvm]"]
# ///
import sys
from xdsl.context import Context
from xdsl.parser import Parser
from xdsl.dialects import get_all_dialects
from xdsl.backend.llvm.convert import convert_module
ctx = Context()
for name, factory in get_all_dialects().items():
ctx.register_dialect(name, factory)
with open(sys.argv[1]) as f:
module = Parser(ctx, f.read()).parse_module()
print(convert_module(module, fallback_target_triple=None))
LLVMTarget
dataclass
¶
convert_module(module: ModuleOp, *, fallback_target_triple: str | None, data_layout: str = '') -> ir.Module
¶
Convert an xDSL module to an LLVM module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
ModuleOp
|
The xDSL module to convert. |
required |
fallback_target_triple
|
str | None
|
The target triple to use when the module does not
carry an
|
required |
data_layout
|
str
|
The data layout to set on the resulting module. If empty, no data layout is set. |
''
|
Returns:
| Type | Description |
|---|---|
Module
|
The corresponding llvmlite IR module. |
Raises:
| Type | Description |
|---|---|
LLVMTranslationException
|
If the |
NotImplementedError
|
If the module contains an op that is not a |
Source code in xdsl/backend/llvm/convert.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |