nvjitlink

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package nvjitlink links device PTX, CUBIN, fatbin, and LTO-IR inputs via libnvJitLink.

This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Input

type Input struct {
	Name string
	Kind rawnvjitlink.InputType
	Data []byte
}

Input is one named device-code input. Kind must describe Data's format. Link copies Data before passing it to the native linker. The caller must not mutate Data concurrently with Link.

func InputFromArtifact

func InputFromArtifact(name string, cm *nvrtc.CompiledModule) (Input, error)

InputFromArtifact selects the linkable output of an NVRTC compilation. It prefers CUBIN for OutputAuto and requires explicit LTO for LTOIR inputs.

func InputFromKernelArtifact

func InputFromKernelArtifact(name string, a *kernel.Artifact) (Input, error)

InputFromKernelArtifact selects the linkable input from a cudakernel.Artifact.

type Option

type Option func(*linkOptions) error

Option configures a link. The zero value is invalid.

func WithArch

func WithArch(arch string) Option

WithArch selects an SM target such as sm_90.

func WithFlag

func WithFlag(flag string) Option

WithFlag adds a documented nvJitLink option.

func WithLTO

func WithLTO() Option

WithLTO enables link-time optimization.

func WithMaxRegisters

func WithMaxRegisters(count int) Option

WithMaxRegisters sets the maximum register count.

func WithPTX

func WithPTX() Option

WithPTX requests PTX output. It requires WithLTO.

func WithVerbose

func WithVerbose() Option

WithVerbose enables linker diagnostic messages.

type Result

type Result struct {
	CUBIN    []byte
	PTX      []byte
	InfoLog  string
	ErrorLog string
	// contains filtered or unexported fields
}

Result owns linked output and diagnostics. Exactly one of CUBIN or PTX is set after successful linking. A non-nil Result may accompany an error so diagnostics remain available; its output must not be used when Link reports an error.

func Link(inputs []Input, options ...string) (*Result, error)

Link synchronously links inputs using native nvJitLink options, including a required -arch= target. The -ptx option selects PTX and requires -lto; otherwise CUBIN is returned. LTOIR inputs require -lto. Toolkit compatibility and unknown options are checked by the loaded linker and reported with its diagnostic log. Link does not install a library or cache results.

func LinkRelocatable

func LinkRelocatable(inputs []Input, options ...string) (*Result, error)

LinkRelocatable links relocatable cubins, as nvcc -rdc writes them, into an executable cubin. It links them in Go with cubin.Link when every input is a cubin, the only option is -arch=sm_<N> naming the inputs' SM, and cubin.Link handles the inputs; otherwise it calls Link. The Result's InfoLog says which linker ran and, when Link ran, why cubin.Link did not.

An error cubin.Link reports in inputs it handles, such as an undefined or duplicate symbol, is returned without calling Link.

Example

This example links two objects that nvcc -rdc built for sm_121a: one calls a function and uses a global the other defines.

package main

import (
	"encoding/hex"
	"fmt"
	"log"
	"os"
	"strings"

	rawnvjitlink "github.com/tmc/lib/nvidia/nvjitlink"
	"github.com/tmc/lib/nvidia/x/compile/nvjitlink"
	"github.com/tmc/lib/nvidia/x/host/cubin"
)

func main() {
	var in []nvjitlink.Input
	for _, name := range []string{"a", "b"} {
		raw, err := os.ReadFile("../../host/cubin/testdata/link/" + name + ".cubin.hex")
		if err != nil {
			log.Fatal(err)
		}
		data, err := hex.DecodeString(strings.Join(strings.Fields(string(raw)), ""))
		if err != nil {
			log.Fatal(err)
		}
		in = append(in, nvjitlink.Input{Name: name + ".o", Kind: rawnvjitlink.InputCubin, Data: data})
	}
	r, err := nvjitlink.LinkRelocatable(in, "-arch=sm_121a")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(r.InfoLog)
	f, err := cubin.Parse(r.CUBIN)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(f.Header.Type, f.Symbol("_Z6helperf").Size)
}
Output:
linked by cubin.Link
ET_EXEC 256

func LinkWithOptions

func LinkWithOptions(inputs []Input, opts ...Option) (*Result, error)

LinkWithOptions links inputs using validated nvJitLink options. Link remains available for callers that pass native option strings.

func (*Result) Close

func (r *Result) Close() error

Close retries native destruction after a cleanup failure. It is safe to call repeatedly or concurrently. nvJitLink binds a handle to the thread that created it, so a retry from another thread may itself fail. Successful Link already closed its native resources. A Result returned with an error must be closed if its initial cleanup failed; until then it retains the linker and input pins.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL