Skip to content

Commit 4ef26c4

Browse files
authored
Merge pull request #312 from maleadt/tb/mem
Wrap IRBuilder methods for memcpy/memmove/memset.
2 parents 37591ff + eaa7cad commit 4ef26c4

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

src/irbuilder.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export ret!, br!, switch!, indirectbr!, invoke!, resume!, unreachable!,
7070

7171
extract_value!, insert_value!,
7272

73-
alloca!, array_alloca!, malloc!, array_malloc!, free!, load!, store!, fence!,
74-
atomic_rmw!, atomic_cmpxchg!, gep!, inbounds_gep!, struct_gep!,
73+
alloca!, array_alloca!, malloc!, array_malloc!, memset!, memcpy!, memmove!, free!,
74+
load!, store!, fence!, atomic_rmw!, atomic_cmpxchg!, gep!, inbounds_gep!, struct_gep!,
7575

7676
trunc!, zext!, sext!, fptoui!, fptosi!, uitofp!, sitofp!, fptrunc!, fpext!,
7777
ptrtoint!, inttoptr!, bitcast!, addrspacecast!, zextorbitcast!, sextorbitcast!,
@@ -234,6 +234,15 @@ malloc!(builder::Builder, Ty::LLVMType, Name::String="") =
234234
array_malloc!(builder::Builder, Ty::LLVMType, Val::Value, Name::String="") =
235235
Instruction(API.LLVMBuildArrayMalloc(builder, Ty, Val, Name))
236236

237+
memset!(builder::Builder, Ptr::Value, Val::Value, Len::Value, Align::Integer) =
238+
Instruction(API.LLVMBuildMemSet(builder, Ptr, Val, Len, Align))
239+
240+
memcpy!(builder::Builder, Dst::Value, DstAlign::Integer, Src::Value, SrcAlign::Integer, Size::Value) =
241+
Instruction(API.LLVMBuildMemCpy(builder, Dst, DstAlign, Src, SrcAlign, Size))
242+
243+
memmove!(builder::Builder, Dst::Value, DstAlign::Integer, Src::Value, SrcAlign::Integer, Size::Value) =
244+
Instruction(API.LLVMBuildMemMove(builder, Dst, DstAlign, Src, SrcAlign, Size))
245+
237246
free!(builder::Builder, PointerVal::Value) =
238247
Instruction(API.LLVMBuildFree(builder, PointerVal))
239248

test/helpers.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
macro check_ir(inst, str)
22
quote
3-
@test occursin($(esc(str)), string($(esc(inst))))
3+
inst = string($(esc(inst)))
4+
@test occursin($(str), inst)
45
end
56
end

test/instructions.jl

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
ft = LLVM.FunctionType(LLVM.VoidType(ctx), [LLVM.Int32Type(ctx), LLVM.Int32Type(ctx),
55
LLVM.FloatType(ctx), LLVM.FloatType(ctx),
66
LLVM.PointerType(LLVM.Int32Type(ctx)),
7-
LLVM.PointerType(LLVM.Int32Type(ctx))])
7+
LLVM.PointerType(LLVM.Int32Type(ctx)),
8+
LLVM.PointerType(LLVM.Int8Type(ctx))])
89
fn = LLVM.Function(mod, "SomeFunction", ft)
910

1011
entrybb = BasicBlock(fn, "entry"; ctx)
@@ -16,7 +17,7 @@
1617
debuglocation!(builder, loc)
1718
@test debuglocation(builder) == loc
1819
debuglocation!(builder)
19-
@test debuglocation(builder) == nothing
20+
@test debuglocation(builder) === nothing
2021

2122
retinst1 = ret!(builder)
2223
@check_ir retinst1 "ret void"
@@ -134,15 +135,30 @@
134135
array_allocainst = array_alloca!(builder, LLVM.Int32Type(ctx), int1)
135136
@check_ir array_allocainst "alloca i32, i32 %0"
136137

137-
# mallocinst = malloc!(builder, LLVM.Int32Type(ctx))
138-
# @check_ir mallocinst "bitcast i8* %malloccall to i32*"
138+
mallocinst = malloc!(builder, LLVM.Int32Type(ctx))
139+
@check_ir mallocinst r"bitcast i8\* %.+ to i32\*"
140+
@check_ir operands(mallocinst)[1] r"call i8\* @malloc\(.+\)"
141+
142+
i8ptr = parameters(fn)[6]
143+
144+
array_mallocinst = array_malloc!(builder, LLVM.Int8Type(ctx), ConstantInt(Int32(42); ctx))
145+
@check_ir array_mallocinst r"call i8\* @malloc\(.+, i32 42\)"
146+
147+
memsetisnt = memset!(builder, i8ptr, ConstantInt(Int8(1); ctx), ConstantInt(Int32(2); ctx), 4)
148+
@check_ir memsetisnt r"call void @llvm.memset.p0i8.i32\(i8\* align 4 %.+, i8 1, i32 2, i1 false\)"
149+
150+
memcpyinst = memcpy!(builder, allocainst, 4, i8ptr, 8, ConstantInt(Int32(32); ctx))
151+
@check_ir memcpyinst r"call void @llvm.memcpy.p0i8.p0i8.i32\(i8\* align 4 %.+, i8\* align 8 %.+, i32 32, i1 false\)"
152+
153+
memmoveinst = memmove!(builder, allocainst, 4, i8ptr, 8, ConstantInt(Int32(32); ctx))
154+
@check_ir memmoveinst r"call void @llvm.memmove.p0i8.p0i8.i32\(i8\* align 4 %.+, i8\* align 8 %.+, i32 32, i1 false\)"
139155

140-
ptr1 = parameters(fn)[5]
156+
i32ptr1 = parameters(fn)[5]
141157

142-
freeinst = free!(builder, ptr1)
158+
freeinst = free!(builder, i32ptr1)
143159
@check_ir freeinst "tail call void @free"
144160

145-
loadinst = load!(builder, ptr1)
161+
loadinst = load!(builder, i32ptr1)
146162
@check_ir loadinst "load i32, i32* %4"
147163
alignment!(loadinst, 4)
148164
@test alignment(loadinst) == 4
@@ -151,16 +167,16 @@
151167
@check_ir loadinst "load atomic i32, i32* %4 seq_cst"
152168
@test ordering(loadinst) == LLVM.API.LLVMAtomicOrderingSequentiallyConsistent
153169

154-
storeinst = store!(builder, int1, ptr1)
170+
storeinst = store!(builder, int1, i32ptr1)
155171
@check_ir storeinst "store i32 %0, i32* %4"
156172

157173
fenceinst = fence!(builder, LLVM.API.LLVMAtomicOrderingNotAtomic)
158174
@check_ir fenceinst "fence"
159175

160-
gepinst = gep!(builder, ptr1, [int1])
176+
gepinst = gep!(builder, i32ptr1, [int1])
161177
@check_ir gepinst "getelementptr i32, i32* %4, i32 %0"
162178

163-
gepinst1 = inbounds_gep!(builder, ptr1, [int1])
179+
gepinst1 = inbounds_gep!(builder, i32ptr1, [int1])
164180
@check_ir gepinst1 "getelementptr inbounds i32, i32* %4, i32 %0"
165181

166182
truncinst = trunc!(builder, int1, LLVM.Int16Type(ctx))
@@ -190,7 +206,7 @@
190206
fpextinst = fpext!(builder, float1, LLVM.DoubleType(ctx))
191207
@check_ir fpextinst "fpext float %2 to double"
192208

193-
ptrtointinst = ptrtoint!(builder, ptr1, LLVM.Int32Type(ctx))
209+
ptrtointinst = ptrtoint!(builder, i32ptr1, LLVM.Int32Type(ctx))
194210
@check_ir ptrtointinst "ptrtoint i32* %4 to i32"
195211

196212
inttoptrinst = inttoptr!(builder, int1, LLVM.PointerType(LLVM.Int32Type(ctx)))
@@ -199,11 +215,11 @@
199215
bitcastinst = bitcast!(builder, int1, LLVM.FloatType(ctx))
200216
@check_ir bitcastinst "bitcast i32 %0 to float"
201217

202-
ptr1typ = llvmtype(ptr1)
203-
ptr2typ = LLVM.PointerType(ptr1typ, 2)
218+
i32ptr1typ = llvmtype(i32ptr1)
219+
i32ptr1typ2 = LLVM.PointerType(eltype(i32ptr1typ), 2)
204220

205-
addrspacecastinst = addrspacecast!(builder, ptr1, ptr2typ)
206-
@check_ir addrspacecastinst "addrspacecast i32* %4 to i32* addrspace(2)*"
221+
addrspacecastinst = addrspacecast!(builder, i32ptr1, i32ptr1typ2)
222+
@check_ir addrspacecastinst "addrspacecast i32* %4 to i32 addrspace(2)*"
207223

208224
zextorbitcastinst = zextorbitcast!(builder, int1, LLVM.FloatType(ctx))
209225
@check_ir zextorbitcastinst "bitcast i32 %0 to float"
@@ -217,9 +233,9 @@
217233
castinst = cast!(builder, LLVM.API.LLVMBitCast, int1, LLVM.FloatType(ctx))
218234
@check_ir castinst "bitcast i32 %0 to float"
219235

220-
ptr3typ = LLVM.PointerType(LLVM.FloatType(ctx))
236+
floatptrtyp = LLVM.PointerType(LLVM.FloatType(ctx))
221237

222-
pointercastinst = pointercast!(builder, ptr1, ptr3typ)
238+
pointercastinst = pointercast!(builder, i32ptr1, floatptrtyp)
223239
@check_ir pointercastinst "bitcast i32* %4 to float*"
224240

225241
intcastinst = intcast!(builder, int1, LLVM.Int64Type(ctx))
@@ -272,10 +288,10 @@
272288
isnotnullinst = isnotnull!(builder, int1)
273289
@check_ir isnotnullinst "icmp ne i32 %0, 0"
274290

275-
ptr2 = parameters(fn)[6]
291+
i32ptr2 = parameters(fn)[6]
276292

277-
ptrdiffinst = ptrdiff!(builder, ptr1, ptr2)
278-
@check_ir ptrdiffinst "sdiv exact i64 %71, ptrtoint (i32* getelementptr (i32, i32* null, i32 1) to i64)"
293+
ptrdiffinst = ptrdiff!(builder, i32ptr1, i32ptr2)
294+
@check_ir ptrdiffinst r"sdiv exact i64 %.+, ptrtoint \(i32\* getelementptr \(i32, i32\* null, i32 1\) to i64\)"
279295

280296
position!(builder)
281297
end

0 commit comments

Comments
 (0)