GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
lto.cc

Fix OpenMP offload link variables for global variables.

Correctly handles OpenMP 'link' clause variables for global variables in LTO builds, preventing duplicate declarations and ensuring host/device consistency.

This commit addresses issues with OpenMP’s declare target directive when used with global variables in link clauses, specifically within the LTO (Link-Time Optimization) process. It prevents redundant creation of device-side pointer variables (...$linkptr) by checking for existing VALUE_EXPRs and ensures that host and device symbols are correctly finalized and outputted by calling varpool_node::finalize_decl and using force_output.

In Details

The offload_handle_link_vars function in lto.cc manages variables specified in OpenMP's link clause for declare target directives. These variables on the host are static, while on the device, they become pointers to the host data (e.g., var becomes *var$linkptr). This patch fixes several issues related to global variables: 1) It avoids multiple calls to build_decl for the same variable by skipping if a VALUE_EXPR already exists. 2) It ensures decl->decl_with_vis.symtab_node is not NULL by calling varpool_node::finalize_decl. 3) It uses force_output to prevent IPA analyse…

For Context
OpenMP
An API that supports multi-platform shared-memory parallel programming of both distributed- and shared-memory architectures. Used here for offloading computations to devices.
declare target
An OpenMP directive that specifies code or data that should be mapped to a target device for offloading.
link clause
An OpenMP clause used with declare target to specify variables that should be linked between the host and the target device.
LTO
Link-Time Optimization. An optimization process that allows the compiler to optimize code across multiple source files during the linking phase, rather than just within individual compilation units.
VALUE_EXPR
An internal GCC representation for an expression whose value is a variable. In this context, it's used to represent the pointer to the host variable on the device.
IPA
Interprocedural Analysis. A set of compiler analyses that examine relationships between different functions in a program to enable optimizations.
Filed Under: openmpltooffload