summaryrefslogtreecommitdiff
path: root/llvm/lib/IR/Core.cpp
AgeCommit message (Collapse)Author
2025-11-12[llvm-c] Add bindings for DbgRecord (#166383)Maxime Arthaud
In the LLVM-C library, there is currently no way to get information about a DbgRecord - which is the new way to attach debug information to llvm instructions. We can only iterate on debug records with LLVMGetFirstDbgRecord/ LLVMGetLastDbgRecord/LLVMGetNextDbgRecord, but there is no way to read information. This PR adds utility functions to read DbgRecord information.
2025-10-13[LLVM-C] Allow `LLVMGetVolatile` to work with any kind of Instruction (#163060)AMS21
Allow LLVMGetVolatile() to work with any kind of Instruction, rather than only memory instructions that accept a volatile flag. For instructions that can never be volatile, the function now return false instead of asserting. This matches the behavior of `Instruction::isVolatile()` in the C++ API.
2025-10-07[LLVM-C] Upstream `LLVMGetOrInsertFunction` from rustc (#162235)AMS21
Add `LLVMGetOrInsertFunction` to the C API as a thin wrapper over `Module::getOrInsertFunction`, upstreamed from [rustc](https://github.com/rust-lang/rust/blob/d773bd07d63a74adcf25ea5f4aae986be94cac5e/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp#L203). It provides a single-call way to get or create a function declaration, avoiding `LLVMGetNamedFunction` + `LLVMAddFunction` and is more idiomatic.
2025-10-05[llvm-c] Add missing nullptr check in LLVMGetFirstDbgRecord (#151101)Maxime Arthaud
I'm using the LLVM C bindings through the llvm-sys rust crate, and noticed that LLVMGetFirstDbgRecord and LLVMGetLastDbgRecord are segfault-ing when called on instructions without debug markers. I found out it's missing a null pointer check. This PR fixes the issue.
2025-08-14Add LLVMGlobalAddDebugInfo to Core.cpp (#148747)peter mckinna
This change allows globals to have multiple metadata attached. The GlobalSetMetadata function only allows only one and is clobbered if more metadata is attempted to be added. The addDebugInfo function calls addMetadata. This is needed because some languages have global structs containing lots of compiler-generated globals.
2025-06-22[C API] Add getter/setter for samesign flag on icmp (#145247)Benji Smith
This was added to the C++ API in https://github.com/llvm/llvm-project/pull/111419 so this change adds accessors in the C API, along with a couple tests.
2025-06-12[DebugInfo][RemoveDIs] Delete debug-info-format flag (#143746)Jeremy Morse
This flag was used to let us incrementally introduce debug records into LLVM, however everything is now using records. It serves no purpose now, so delete it.
2025-06-09Remove GlobalObject::getAlign/setAlignment (#143188)Eli Friedman
Currently, GlobalObject has an "alignment" property... but it's basically nonsense: alignment doesn't mean the same thing for variables and functions, and it's completely meaningless for ifuncs. This "removes" (actually marking protected) the methods from GlobalObject, adds the relevant methods to Function and GlobalVariable, and adjusts the code appropriately. This should make future alignment-related cleanups easier.
2025-05-10[IR] Teach getAsmString to return StringRef (NFC) (#139406)Kazu Hirata
This is for consistency with #139401.
2025-05-10[IR] Teach getConstraintString to return StringRef (NFC) (#139401)Kazu Hirata
With this change, some callers get to use StringRef::starts_with. I'm planning to teach getAsmString to return StringRef also, but I'ld like to keep that separate from this patch.
2025-04-30Reland [llvm] Add support for llvm IR atomicrmw fminimum/fmaximum ↵Jonathan Thackray
instructions (#137701) This patch adds support for LLVM IR atomicrmw `fmaximum` and `fminimum` instructions. These mirror the `llvm.maximum.*` and `llvm.minimum.*` instructions, but are atomic and use IEEE754 2019 handling for NaNs, which is different to `fmax` and `fmin`. See: https://llvm.org/docs/LangRef.html#llvm-minimum-intrinsic for more details. Future changes will allow this LLVM IR to be lowered to specialised assembler instructions on suitable targets, such as AArch64.
2025-04-28Revert "[llvm] Add support for llvm IR atomicrmw fminimum/fmaximum ↵Jonathan Thackray
instructions" (#137657) Reverts llvm/llvm-project#136759 due to bad interaction with c792b25e4
2025-04-28[llvm] Add support for llvm IR atomicrmw fminimum/fmaximum instructions ↵Jonathan Thackray
(#136759) This patch adds support for LLVM IR atomicrmw `fmaximum` and `fminimum` instructions. These mirror the `llvm.maximum.*` and `llvm.minimum.*` instructions, but are atomic and use IEEE754 2019 handling for NaNs, which is different to `fmax` and `fmin`. See: https://llvm.org/docs/LangRef.html#llvm-minimum-intrinsic for more details. Future changes will allow this LLVM IR to be lowered to specialised assembler instructions on suitable targets, such as AArch64.
2025-04-25[llvm-c] Add `LLVMConstDataArray` and `LLVMGetRawDataValues` (#129440)Quinton Miller
Resolves #129439. The addition to `echo.ll` is for testing `ConstantArray`, because every other array in that file is in fact a `ConstantDataArray` and now takes the new code path in `echo.cpp`.
2025-03-06[IR] Store Triple in Module (NFC) (#129868)Nikita Popov
The module currently stores the target triple as a string. This means that any code that wants to actually use the triple first has to instantiate a Triple, which is somewhat expensive. The change in #121652 caused a moderate compile-time regression due to this. While it would be easy enough to work around, I think that architecturally, it makes more sense to store the parsed Triple in the module, so that it can always be directly queried. For this change, I've opted not to add any magic conversions between std::string and Triple for backwards-compatibilty purses, and instead write out needed Triple()s or str()s explicitly. This is because I think a decent number of them should be changed to work on Triple as well, to avoid unnecessary conversions back and forth. The only interesting part in this patch is that the default triple is Triple("") instead of Triple() to preserve existing behavior. The former defaults to using the ELF object format instead of unknown object format. We should fix that as well.
2025-02-14[IR] Remove mul constant expression (#127046)Nikita Popov
Remove support for the mul constant expression, which has previously already been marked as undesirable. This removes the APIs to create mul expressions and updates tests to stop using mul expressions. Part of: https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179
2025-01-21[IR] Replace of PointerType::get(Type) with opaque version (NFC) (#123617)Mats Jun Larsen
In accordance with https://github.com/llvm/llvm-project/issues/123569 In order to keep the patch at reasonable size, this PR only covers for the llvm subproject, unittests excluded.
2024-11-04[LLVM] Change `LLVMIntrinsicCopyOverloadedName` API return type (#114334)Rahul Joshi
Change the return type of `LLVMIntrinsicCopyOverloadedName` and `LLVMIntrinsicCopyOverloadedName2` to `char *` instead of `const char *` since the returned memory is owned by the caller and we expect that the returned pointer is passed to free to deallocate it (without casting it back to non-const pointer).
2024-10-11[NFC] Rename `Intrinsic::getDeclaration` to `getOrInsertDeclaration` (#111752)Rahul Joshi
Rename the function to reflect its correct behavior and to be consistent with `Module::getOrInsertFunction`. This is also in preparation of adding a new `Intrinsic::getDeclaration` that will have behavior similar to `Module::getFunction` (i.e, just lookup, no creation).
2024-09-30[NFC] Move intrinsic related functions to Intrinsic namespace (#110125)Rahul Joshi
Move static functions `Function::lookupIntrinsicID` and `Function::isTargetIntrinsic` to Intrinsic namespace.
2024-09-23[IRBuilder] Remove uses of CreateGlobalStringPtr() (NFC)Nikita Popov
Since the migration to opaque pointers, CreateGlobalStringPtr() is the same as CreateGlobalString(). Normalize to the latter.
2024-09-23[C API] Add usub_cond and usub_sat atomic ops to C API (#109532)Benji Smith
These were added in the C++ API in https://github.com/llvm/llvm-project/pull/105568 but were not exposed via the C API previously
2024-09-20[LLVM-C] Add bindings to `Instruction::getDbgRecordRange()` (#107802)Michal Rostecki
Since the migration from `@llvm.dbg.value` intrinsic to `#dbg_value` records, there is no way to retrieve the debug records for an `Instruction` in LLVM-C API. Previously, with debug info intrinsics, retrieving debug info for an `Instruction` could be done with `LLVMGetNextInstructions`, because the intrinsic call was also an instruction. However, to be able to retrieve debug info with the current LLVM, where debug records are used, the `getDbgRecordRange()` iterator needs to be exposed. Add new functions for DbgRecord sequence traversal: LLVMGetFirstDbgRecord LLVMGetLastDbgRecord LLVMGetNextDbgRecord LLVMGetPreviousDbgRecord See llvm/docs/RemoveDIsDebugInfo.md and release notes.
2024-08-28[LLVM][C API] Clearing initializer and personality by passing NULL (#105521)Tim Besard
This is similar to how the C++ API supports passing `nullptr` to `setPersonalityFn` or `setInitializer`.
2024-08-20[LLVM] Add a C API for creating instructions with custom syncscopes. (#104775)Tim Besard
Another upstreaming of C API extensions we have in Julia/LLVM.jl. Although [we went](https://github.com/maleadt/LLVM.jl/pull/431) with a string-based API there, here I'm proposing something that's similar to existing metadata/attribute APIs: - explicit functions to map syncscope names to IDs, and back - `LLVM*SyncScope` versions of builder APIs that already take a `SingleThread` argument: atomic rmw, atomic xchg, fence - `LLVMGetAtomicSyncScopeID` and `LLVMSetAtomicSyncScopeID` for other atomic instructions - testing through `llvm-c-test`'s `--echo` functionality
2024-08-20[llvm-c] Add getters for LLVMContextRef for various types (#99087)Bogdan-Alexandru Geană
Small PR to add additional getters for LLVMContextRef in the C API.
2024-08-16[llvm-c] Add non-cstring versions of LLVMGetNamedFunction and ↵wr7
LLVMGetNamedGlobal (#103396) Add `LLVMGetNamedFunctionWithLength` and `LLVMGetNamedGlobalWithLength` As far as i know, it isn't currently possible to use `LLVMGetNamedFunction` and `LLVMGetNamedGlobal` with non-null-terminated strings. These new functions are more convenient for C programs that use non-null-terminated strings or for languages like Rust that primarily use non-null-terminated strings.
2024-07-25Remove the `x86_mmx` IR type. (#98505)James Y Knight
It is now translated to `<1 x i64>`, which allows the removal of a bunch of special casing. This _incompatibly_ changes the ABI of any LLVM IR function with `x86_mmx` arguments or returns: instead of passing in mmx registers, they will now be passed via integer registers. However, the real-world incompatibility caused by this is expected to be minimal, because Clang never uses the x86_mmx type -- it lowers `__m64` to either `<1 x i64>` or `double`, depending on ABI. This change does _not_ eliminate the SelectionDAG `MVT::x86mmx` type. That type simply no longer corresponds to an IR type, and is used only by MMX intrinsics and inline-asm operands. Because SelectionDAGBuilder only knows how to generate the operands/results of intrinsics based on the IR type, it thus now generates the intrinsics with the type MVT::v1i64, instead of MVT::x86mmx. We need to fix this before the DAG LegalizeTypes, and thus have the X86 backend fix them up in DAGCombine. (This may be a short-lived hack, if all the MMX intrinsics can be removed in upcoming changes.) Works towards issue #98272.
2024-07-17[C API] Support new ptrauth constant type (#93909)Benji Smith
This is a new constant type that was added to the C++ API in 0edc97f119f3ac3ff96b11183fe5c001a48a9a8d. This adds the ability to create instances of this constant and get its values to the C API.
2024-07-16[C API] Add accessors for new no-wrap flags on GEP instructions (#97970)Benji Smith
Previously, only the inbounds flag was accessible via the C API. This adds support for any no-wrap related flags (currently nuw and nusw).
2024-06-26[C API] Add getters for Target Extension Types to C API (#96447)Benji Smith
Accessors for the name, type parameters, and integer parameters are added. A test is added to echo.ll This was originally done in https://github.com/llvm/llvm-project/pull/71291 but that has been stale for several months. This re-applies the changes, but with some tweaks. e.g. removing the bulk getters in favour of a simple get-by-index approach for the type/integer parameters. The latter is more in line with the rest of the API
2024-06-24Revert "[IR][NFC] Update IRBuilder to use InsertPosition (#96497)"Stephen Tozer
Reverts the above commit, as it updates a common header function and did not update all callsites: https://lab.llvm.org/buildbot/#/builders/29/builds/382 This reverts commit 6481dc57612671ebe77fe9c34214fba94e1b3b27.
2024-06-24[IR][NFC] Update IRBuilder to use InsertPosition (#96497)Stephen Tozer
Uses the new InsertPosition class (added in #94226) to simplify some of the IRBuilder interface, and removes the need to pass a BasicBlock alongside a BasicBlock::iterator, using the fact that we can now get the parent basic block from the iterator even if it points to the sentinel. This patch removes the BasicBlock argument from each constructor or call to setInsertPoint. This has no functional effect, but later on as we look to remove the `Instruction *InsertBefore` argument from instruction-creation (discussed [here](https://discourse.llvm.org/t/psa-instruction-constructors-changing-to-iterator-only-insertion/77845)), this will simplify the process by allowing us to deprecate the InsertPosition constructor directly and catch all the cases where we use instructions rather than iterators.
2024-06-20[IR] Remove support for shl constant expressions (#96037)Nikita Popov
Remove support for shl constant expressions, as part of: https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179
2024-06-10[RemoveDIs] C API: Add before-dbg-record versions of IRBuilder position ↵Orlando Cazalet-Hyams
funcs (#92417) Add `LLVMPositionBuilderBeforeDbgRecords` and `LLVMPositionBuilderBeforeInstrAndDbgRecords` to `llvm/include/llvm-c/Core.h` which behave the same as `LLVMPositionBuilder` and `LVMPositionBuilderBefore` except that the position is set before debug records attached to the target instruction (the existing functions set the insertion point to after any attached debug records). More info on debug records and the migration towards using them can be found here: https://llvm.org/docs/RemoveDIsDebugInfo.html The distinction is important in some situations. An important example is when inserting a phi before another instruction which has debug records attached to it (these come "before" the instruction). Inserting before the instruction but after the debug records would result in having debug records before a phi, which is illegal. That results in an assertion failure: `llvm/lib/IR/Instruction.cpp:166: Assertion '!isa<PHINode>(this) && "Inserting PHI after debug-records!"' failed.` In llvm (C++) we've added bit to instruction iterators that carries around the extra information. Adding dedicated functions seemed like the least invasive and least suprising way to update the C API. Update llvm/tools/llvm-c-test/debuginfo.c to test this functionality. Update the OCaml bindings, the migration docs and release notes.
2024-06-04[IR] Remove support for icmp and fcmp constant expressions (#93038)Nikita Popov
Remove support for the icmp and fcmp constant expressions. This is part of: https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179 As usual, many of the updated tests will no longer test what they were originally intended to -- this is hard to preserve when constant expressions get removed, and in many cases just impossible as the existence of a specific kind of constant expression was the cause of the issue in the first place.
2024-05-08[C API] Add getters and build function for CallBr (#91154)Benji Smith
This adds LLVMBuildCallBr to create CallBr instructions, and getters for the CallBr-specific data. The remainder of its data, e.g. arguments/function, can be accessed using existing getters.
2024-05-04[C API] Add function to create ConstantRange attributes to C API (#90505)Andreas Jonson
2024-04-16[C API] Support uinc_wrap/udec_wrap in atomicrmw when accessing the bin op ↵Benji Smith
(#87163) These previously were added in the C++ API in 778cf5431cafc243f81dd5c8cbd27701ff7f9120, but without updating the enum in the C API or mapping functions. Corresponding tests for all current atomicrmw bin ops have been added as well.
2024-03-28[RemoveDIs] Update DIBuilder C API and OCaml bindings [2/2] (#86529)Orlando Cazalet-Hyams
Follow on from #84915 which adds the DbgRecord function variants. The C API changes were reviewed in #85657. # C API Update the LLVMDIBuilderInsert... functions to insert DbgRecords instead of debug intrinsics. LLVMDIBuilderInsertDeclareBefore LLVMDIBuilderInsertDeclareAtEnd LLVMDIBuilderInsertDbgValueBefore LLVMDIBuilderInsertDbgValueAtEnd Calling these functions will now cause an assertion if the module is in the wrong debug info format. They should only be used when the module is in "new debug format". Use LLVMIsNewDbgInfoFormat to query and LLVMSetIsNewDbgInfoFormat to change the debug info format of a module. Please see https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-change (RemoveDIsDebugInfo.md) for more info. # OCaml bindings Add set_is_new_dbg_info_format and is_new_dbg_info_format to the OCaml bindings. These can be used to set and query the current debug info mode. These will eventually be removed, but are useful while we're transitioning between old and new debug info formats. Add string_of_lldbgrecord, like string_of_llvalue but prints DbgRecords. In test dbginfo.ml, unconditionally set the module debug info to the new mode and update CHECK lines to check for DbgRecords. Without this change the test crashes because it attempts to insert DbgRecords (new default behaviour of llvm_dibuild_insert_declare_...) into a module that is in the old debug info mode.
2024-03-26[LLVM] Remove nuw neg (#86295)Yingwei Zheng
This patch removes APIs that creating NUW neg. It is a trivial case because `sub nuw 0, X` always gets simplified into zero. I believe there is no optimization opportunities in the real-world applications that we can take advantage of the nuw flag. Motivated by https://github.com/llvm/llvm-project/pull/84792#discussion_r1524891134. Compile-time improvement: https://llvm-compile-time-tracker.com/compare.php?from=d1f182c895728d89c5c3d198b133e212a5d9d4a3&to=da7b7478b7cbb32c09d760f6b8d0e67901e0d533&stat=instructions:u
2024-03-18[C API] Add accessors for function prefix and prologue data (#82193)Benji Smith
A test is added to echo.ll, and the echo.cpp part of llvm-c-test is updated to clone a function's prefix and prologue.
2024-03-18[RemoveDIs] Update DIBuilder C API with DbgRecord functions [1/2] (#84915)Orlando Cazalet-Hyams
Follow on from #84739, which updates the DIBuilder class. All the functions that have been added are temporary and will be deprecated in the future. The intention is that they'll help downstream projects adapt during the transition period. ``` New functions (all to be deprecated) ------------------------------------ LLVMIsNewDbgInfoFormat # Returns true if the module is in the new non-instruction mode. LLVMSetIsNewDbgInfoFormat # Convert to the requested debug info format. LLVMDIBuilderInsertDeclareIntrinsicBefore # Insert a debug intrinsic (old debug info format). LLVMDIBuilderInsertDeclareIntrinsicAtEnd # Same as above. LLVMDIBuilderInsertDbgValueIntrinsicBefore # Same as above. LLVMDIBuilderInsertDbgValueIntrinsicAtEnd # Same as above. LLVMDIBuilderInsertDeclareRecordBefore # Insert a debug record (new debug info format). LLVMDIBuilderInsertDeclareRecordAtEnd # Same as above. LLVMDIBuilderInsertDbgValueRecordBefore # Same as above. LLVMDIBuilderInsertDbgValueRecordAtEnd # Same as above. ``` The existing `LLVMDIBuilderInsert...` functions call through to the intrinsic versions (old debug info format) currently. In the next patch, I'll swap them to call the debug records versions (new debug info format). Downstream users of this API can query and change the current format using the first two functions above, or can instead opt to temporarily use intrinsics or records explicitly.
2024-03-09[llvm-c] Add C API methods to match size_t ConstantDataArray C++ API ↵erer1243
signatures (#84433) Adds `LLVMConstStringInContext2` and `LLVMConstString2`, which are identical to originals except that they use `size_t` for length. This is a clone of https://github.com/llvm/llvm-project/commit/35276f16e5a2cae0dfb49c0fbf874d4d2f177acc and is needed for https://github.com/rust-lang/rust/pull/122000. As an aside, the issue of 32 bit overflow on constants is present in the C++ APIs as well. A few classes, e.g. `ConstantDataArray` and `ConstantAggregateZero`, can hold 64-bit ArrayTypes but their length accessors return 32-bit values. This means the same issue from the original Rust report is also present in LLVM itself. Would it be a reasonable goal to update all of these length methods & types to be uint64_t, or would that be too breaking? Alternatively, we could use safe fallible casts instead of implicit ones inside the accessors (if an overflow does happen, the solution would be to use `MyValue->getType()->getArrayNumElements()` instead).
2024-02-12[C API] Add blockaddress getters to C API (#81382)Benji Smith
This allows for accessing the function/basic block that a blockaddress constant refers to Due to the difficulties of fully supporting cloning BlockAddress values in echo.cpp, tests are instead done using a unit test. This previously was up for review at https://github.com/llvm/llvm-project/pull/77390.
2023-12-12[C API] Add getters and setters for fast-math flags on relevant instructions ↵Benji Smith
(#75123) These flags are usable on floating point arithmetic, as well as call, select, and phi instructions whose resulting type is floating point, or a vector of, or an array of, a valid type. Whether or not the flags are valid for a given instruction can be checked with the new LLVMCanValueUseFastMathFlags function. These are exposed using a new LLVMFastMathFlags type, which is an alias for unsigned. An anonymous enum defines the bit values for it. Tests are added in echo.ll for select/phil/call, and the floating point types in the new float_ops.ll bindings test. Select and the floating point arithmetic instructions were not implemented in llvm-c-test/echo.cpp, so they were added as well.
2023-12-11[LLVM-C] Support operand bundles (#73914)Quinton Miller
Added the following functions for manipulating operand bundles, as well as building ``call`` and ``invoke`` instructions that use operand bundles: * LLVMBuildCallWithOperandBundles * LLVMBuildInvokeWithOperandBundles * LLVMCreateOperandBundle * LLVMDisposeOperandBundle * LLVMGetNumOperandBundles * LLVMGetOperandBundleAtIndex * LLVMGetNumOperandBundleArgs * LLVMGetOperandBundleArgAtIndex * LLVMGetOperandBundleTag Fixes #71873.
2023-12-06[llvm-c] Add support for setting/getting new disjoint flag on or ↵Alex Bradbury
instructions (#74517) Follows #73952 doing the same thing for the nneg flag on zext (i.e., exposing support in the C API).
2023-11-29[C API] Add support for setting/getting new nneg flag on zext instructions ↵Benji Smith
(#73592) This flag was added in #67982, but was not yet accessible via the C API. This commit adds a getter/setter for this flag, and a test for it.
2023-11-20[llvm-c] Fix outdated comment (NFC)Nikita Popov
Use the function value type instead of the element type of the function pointer type. Fixes https://github.com/llvm/llvm-project/issues/72798.