|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE |
| 3 | + |
| 4 | +from typing import Sequence |
| 5 | + |
| 6 | +_PRECHECK_NVVM_IR = """target triple = "nvptx64-unknown-cuda" |
| 7 | +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64" |
| 8 | +
|
| 9 | +define void @dummy_kernel() {{ |
| 10 | +entry: |
| 11 | + ret void |
| 12 | +}} |
| 13 | +
|
| 14 | +!nvvm.annotations = !{{!0}} |
| 15 | +!0 = !{{void ()* @dummy_kernel, !"kernel", i32 1}} |
| 16 | +
|
| 17 | +!nvvmir.version = !{{!1}} |
| 18 | +!1 = !{{i32 {major}, i32 {minor}, i32 {debug_major}, i32 {debug_minor}}} |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +def check_nvvm_compiler_options(options: Sequence[str]) -> bool: |
| 23 | + """ |
| 24 | + Abstracted from https://github.com/NVIDIA/numba-cuda/pull/681 |
| 25 | +
|
| 26 | + Check if the specified options are supported by the current libNVVM version. |
| 27 | +
|
| 28 | + The options are a list of strings, each representing a compiler option. |
| 29 | +
|
| 30 | + If the test program fails to compile, the options are not supported and False |
| 31 | + is returned. |
| 32 | +
|
| 33 | + If the test program compiles successfully, True is returned. |
| 34 | +
|
| 35 | + cuda.bindings.nvvm returns exceptions instead of return codes. |
| 36 | +
|
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + options : Sequence[str] |
| 40 | + List of compiler options as strings (e.g., ["-arch=compute_90", "-g"]). |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + bool |
| 45 | + True if the options are supported, False otherwise. |
| 46 | +
|
| 47 | + Examples |
| 48 | + -------- |
| 49 | + >>> from cuda.bindings.utils import check_nvvm_compiler_options |
| 50 | + >>> check_nvvm_compiler_options(["-arch=compute_90", "-g"]) |
| 51 | + True |
| 52 | + """ |
| 53 | + try: |
| 54 | + from cuda.bindings import nvvm |
| 55 | + except ModuleNotFoundError as exc: |
| 56 | + if exc.name == "nvvm": |
| 57 | + return False |
| 58 | + raise |
| 59 | + |
| 60 | + from cuda.bindings._internal.nvvm import _inspect_function_pointer |
| 61 | + |
| 62 | + if _inspect_function_pointer("__nvvmCreateProgram") == 0: |
| 63 | + return False |
| 64 | + |
| 65 | + program = nvvm.create_program() |
| 66 | + try: |
| 67 | + major, minor, debug_major, debug_minor = nvvm.ir_version() |
| 68 | + precheck_ir = _PRECHECK_NVVM_IR.format( |
| 69 | + major=major, |
| 70 | + minor=minor, |
| 71 | + debug_major=debug_major, |
| 72 | + debug_minor=debug_minor, |
| 73 | + ) |
| 74 | + precheck_ir_bytes = precheck_ir.encode("utf-8") |
| 75 | + nvvm.add_module_to_program( |
| 76 | + program, |
| 77 | + precheck_ir_bytes, |
| 78 | + len(precheck_ir_bytes), |
| 79 | + "precheck.ll", |
| 80 | + ) |
| 81 | + try: |
| 82 | + nvvm.compile_program(program, len(options), options) |
| 83 | + except nvvm.nvvmError as e: |
| 84 | + if e.status == nvvm.Result.ERROR_INVALID_OPTION: |
| 85 | + return False |
| 86 | + raise |
| 87 | + finally: |
| 88 | + nvvm.destroy_program(program) |
| 89 | + return True |
0 commit comments