Skip to content

Commit b8d15e3

Browse files
committed
Add bindings to common memory intrinsics
1 parent b0cd8fd commit b8d15e3

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,100 @@ extension IRBuilder {
14421442
}
14431443
}
14441444

1445+
// MARK: Memory Intrinsics
1446+
1447+
extension IRBuilder {
1448+
/// Builds a call to the `llvm.memset.*` family of intrinsics to fill a
1449+
/// given block of memory with a given byte value.
1450+
///
1451+
/// - NOTE: Unlike the standard function `memset` defined in libc,
1452+
/// `llvm.memset` does not return a value and may be volatile. The address
1453+
/// space of the source and destination values need not match.
1454+
///
1455+
/// - Parameters:
1456+
/// - dest: A pointer value to the destination that will be filled.
1457+
/// - value: A byte value to fill the destination with.
1458+
/// - length: The number of bytes to fill.
1459+
/// - alignment: The alignment of the destination pointer value.
1460+
/// - volatile: If true, builds a volatile `llvm.memset` intrinsic, else
1461+
/// builds a non-volatile `llvm.memset` instrinsic. The exact behavior of
1462+
/// volatile memory intrinsics is platform-dependent and should not be
1463+
/// relied upon to perform consistently. For more information, see the
1464+
/// language reference's section on [Volatile Memory
1465+
/// Access](http://llvm.org/docs/LangRef.html#volatile-memory-accesses).
1466+
public func buildMemset(
1467+
to dest: IRValue, of value: IRValue, length: IRValue,
1468+
alignment: Alignment, volatile: Bool = false
1469+
) {
1470+
let instruction = LLVMBuildMemSet(self.llvm, dest.asLLVM(), value.asLLVM(), length.asLLVM(), alignment.rawValue)
1471+
LLVMSetVolatile(instruction, volatile.llvm)
1472+
}
1473+
1474+
/// Builds a call to the `llvm.memcpy.*` family of intrinsics to copy a block
1475+
/// of memory to a given destination memory location from a given source
1476+
/// memory location.
1477+
///
1478+
/// - WARNING: It is illegal for the destination and source locations to
1479+
/// overlap each other.
1480+
///
1481+
/// - NOTE: Unlike the standard function `memcpy` defined in libc,
1482+
/// `llvm.memcpy` does not return a value and may be volatile. The address
1483+
/// space of the source and destination values need not match.
1484+
///
1485+
/// - Parameters:
1486+
/// - dest: A pointer to the destination that will be filled.
1487+
/// - destAlign: The alignment of the destination pointer value.
1488+
/// - src: A pointer to the source that will be copied from.
1489+
/// - srcAlign: The alignment of the source pointer value.
1490+
/// - length: The number of bytes to fill.
1491+
/// - volatile: If true, builds a volatile `llvm.memcpy` intrinsic, else
1492+
/// builds a non-volatile `llvm.memcpy` instrinsic. The exact behavior of
1493+
/// volatile memory intrinsics is platform-dependent and should not be
1494+
/// relied upon to perform consistently. For more information, see the
1495+
/// language reference's section on [Volatile Memory
1496+
/// Access](http://llvm.org/docs/LangRef.html#volatile-memory-accesses).
1497+
public func buildMemCpy(
1498+
to dest: IRValue, _ destAlign: Alignment,
1499+
from src: IRValue, _ srcAlign: Alignment,
1500+
length: IRValue, volatile: Bool = false
1501+
) {
1502+
let instruction = LLVMBuildMemCpy(self.llvm, dest.asLLVM(), destAlign.rawValue, src.asLLVM(), srcAlign.rawValue, length.asLLVM())
1503+
LLVMSetVolatile(instruction, volatile.llvm)
1504+
}
1505+
1506+
/// Builds a call to the `llvm.memmove.*` family of intrinsics to move a
1507+
/// block of memory to a given destination memory location from a given source
1508+
/// memory location.
1509+
///
1510+
/// Unlike `llvm.memcpy.*`, the destination and source memory locations may
1511+
/// overlap with each other.
1512+
///
1513+
/// - NOTE: Unlike the standard function `memmove` defined in libc,
1514+
/// `llvm.memmove` does not return a value and may be volatile. The address
1515+
/// space of the source and destination values need not match.
1516+
///
1517+
/// - Parameters:
1518+
/// - dest: A pointer to the destination that will be filled.
1519+
/// - destAlign: The alignment of the destination pointer value.
1520+
/// - src: A pointer to the source that will be copied from.
1521+
/// - srcAlign: The alignment of the source pointer value.
1522+
/// - length: The number of bytes to fill.
1523+
/// - volatile: If true, builds a volatile `llvm.memmove` intrinsic, else
1524+
/// builds a non-volatile `llvm.memmove` instrinsic. The exact behavior of
1525+
/// volatile memory intrinsics is platform-dependent and should not be
1526+
/// relied upon to perform consistently. For more information, see the
1527+
/// language reference's section on [Volatile Memory
1528+
/// Access](http://llvm.org/docs/LangRef.html#volatile-memory-accesses).
1529+
public func buildMemMove(
1530+
to dest: IRValue, _ destAlign: Alignment,
1531+
from src: IRValue, _ srcAlign: Alignment,
1532+
length: IRValue, volatile: Bool = false
1533+
) {
1534+
let instruction = LLVMBuildMemMove(self.llvm, dest.asLLVM(), destAlign.rawValue, src.asLLVM(), srcAlign.rawValue, length.asLLVM())
1535+
LLVMSetVolatile(instruction, volatile.llvm)
1536+
}
1537+
}
1538+
14451539
// MARK: Aggregate Instructions
14461540

14471541
extension IRBuilder {

0 commit comments

Comments
 (0)