Skip to content

Commit 5df309c

Browse files
committed
compiler: combine struct padding zeroing logic
1 parent 56c05c3 commit 5df309c

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

compiler/map.go

+15-22
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,6 @@ func (b *builder) zeroUndefBytes(llvmType llvm.Type, ptr llvm.Value) error {
288288
llvmArrayType := llvmType
289289
llvmElemType := llvmType.ElementType()
290290

291-
zero := llvm.ConstInt(b.ctx.Int32Type(), 0, false)
292-
293291
for i := 0; i < llvmArrayType.ArrayLength(); i++ {
294292
idx := llvm.ConstInt(b.uintptrType, uint64(i), false)
295293
elemPtr := b.CreateInBoundsGEP(llvmArrayType, ptr, []llvm.Value{zero, idx}, "")
@@ -312,29 +310,24 @@ func (b *builder) zeroUndefBytes(llvmType llvm.Type, ptr llvm.Value) error {
312310
b.zeroUndefBytes(llvmElemType, elemPtr)
313311

314312
// zero any padding bytes before the next field, if any
313+
offset := b.targetData.ElementOffset(llvmStructType, i)
314+
storeSize := b.targetData.TypeStoreSize(llvmElemType)
315+
fieldEndOffset := offset + storeSize
316+
317+
var nextOffset uint64
315318
if i < numFields-1 {
316-
offset := b.targetData.ElementOffset(llvmStructType, i)
317-
nextOffset := b.targetData.ElementOffset(llvmStructType, i+1)
318-
if storeSize := b.targetData.TypeStoreSize(llvmElemType); offset+storeSize != nextOffset {
319-
n := llvm.ConstInt(b.uintptrType, nextOffset-(offset+storeSize), false)
320-
llvmStoreSize := llvm.ConstInt(b.uintptrType, storeSize, false)
321-
elemPtr = b.CreateBitCast(elemPtr, b.i8ptrType, "")
322-
paddingStart := b.CreateInBoundsGEP(b.i8ptrType, elemPtr, []llvm.Value{llvmStoreSize}, "")
323-
b.createRuntimeCall("memzero", []llvm.Value{paddingStart, n}, "")
324-
}
319+
nextOffset = b.targetData.ElementOffset(llvmStructType, i+1)
320+
} else {
321+
// Last field? Next offset is the total size of the allcoate struct.
322+
nextOffset = b.targetData.TypeAllocSize(llvmStructType)
325323
}
326-
}
327324

328-
// zero any padding bytes between end of last field and end of struct
329-
allocSize := b.targetData.TypeAllocSize(llvmStructType)
330-
lastElemOffset := b.targetData.ElementOffset(llvmStructType, numFields-1)
331-
lastElemStore := b.targetData.TypeStoreSize(llvmElementTypes[numFields-1])
332-
333-
if allocSize != lastElemOffset+lastElemStore {
334-
n := llvm.ConstInt(b.uintptrType, allocSize-(lastElemOffset+lastElemStore), false)
335-
llvmStoreSize := llvm.ConstInt(b.uintptrType, lastElemOffset+lastElemStore, false)
336-
paddingStart := b.CreateInBoundsGEP(b.ctx.Int8Type(), ptr, []llvm.Value{llvmStoreSize}, "")
337-
b.createRuntimeCall("memzero", []llvm.Value{paddingStart, n}, "")
325+
if fieldEndOffset != nextOffset {
326+
n := llvm.ConstInt(b.uintptrType, nextOffset-fieldEndOffset, false)
327+
llvmStoreSize := llvm.ConstInt(b.uintptrType, storeSize, false)
328+
paddingStart := b.CreateInBoundsGEP(b.i8ptrType, elemPtr, []llvm.Value{llvmStoreSize}, "")
329+
b.createRuntimeCall("memzero", []llvm.Value{paddingStart, n}, "")
330+
}
338331
}
339332
}
340333

compiler/testdata/zeromap.ll

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ entry:
3333
store %main.hasPadding %2, ptr %hashmap.key, align 8
3434
%3 = getelementptr inbounds ptr, ptr %hashmap.key, i32 1
3535
call void @runtime.memzero(ptr nonnull %3, i32 3, ptr undef) #4
36-
%4 = getelementptr inbounds i8, ptr %hashmap.key, i32 9
36+
%4 = getelementptr inbounds { i1, i32, i1 }, ptr %hashmap.key, i32 1
3737
call void @runtime.memzero(ptr nonnull %4, i32 3, ptr undef) #4
3838
%5 = call i1 @runtime.hashmapBinaryGet(ptr %m, ptr nonnull %hashmap.key, ptr nonnull %hashmap.value, i32 4, ptr undef) #4
3939
call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %hashmap.key)
@@ -71,7 +71,7 @@ entry:
7171
store %main.hasPadding %2, ptr %hashmap.key, align 8
7272
%3 = getelementptr inbounds ptr, ptr %hashmap.key, i32 1
7373
call void @runtime.memzero(ptr nonnull %3, i32 3, ptr undef) #4
74-
%4 = getelementptr inbounds i8, ptr %hashmap.key, i32 9
74+
%4 = getelementptr inbounds { i1, i32, i1 }, ptr %hashmap.key, i32 1
7575
call void @runtime.memzero(ptr nonnull %4, i32 3, ptr undef) #4
7676
call void @runtime.hashmapBinarySet(ptr %m, ptr nonnull %hashmap.key, ptr nonnull %hashmap.value, ptr undef) #4
7777
call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %hashmap.key)
@@ -106,11 +106,11 @@ entry:
106106
store %main.hasPadding %s.elt9, ptr %hashmap.key.repack8, align 4
107107
%0 = getelementptr inbounds ptr, ptr %hashmap.key, i32 1
108108
call void @runtime.memzero(ptr nonnull %0, i32 3, ptr undef) #4
109-
%1 = getelementptr inbounds i8, ptr %hashmap.key, i32 9
109+
%1 = getelementptr inbounds %main.hasPadding, ptr %hashmap.key, i32 1
110110
call void @runtime.memzero(ptr nonnull %1, i32 3, ptr undef) #4
111111
%2 = getelementptr inbounds [2 x %main.hasPadding], ptr %hashmap.key, i32 0, i32 1, i32 1
112112
call void @runtime.memzero(ptr nonnull %2, i32 3, ptr undef) #4
113-
%3 = getelementptr inbounds i8, ptr %hashmap.key, i32 21
113+
%3 = getelementptr inbounds [2 x %main.hasPadding], ptr %hashmap.key, i32 1
114114
call void @runtime.memzero(ptr nonnull %3, i32 3, ptr undef) #4
115115
%4 = call i1 @runtime.hashmapBinaryGet(ptr %m, ptr nonnull %hashmap.key, ptr nonnull %hashmap.value, i32 4, ptr undef) #4
116116
call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %hashmap.key)
@@ -145,11 +145,11 @@ entry:
145145
store %main.hasPadding %s.elt9, ptr %hashmap.key.repack8, align 4
146146
%0 = getelementptr inbounds ptr, ptr %hashmap.key, i32 1
147147
call void @runtime.memzero(ptr nonnull %0, i32 3, ptr undef) #4
148-
%1 = getelementptr inbounds i8, ptr %hashmap.key, i32 9
148+
%1 = getelementptr inbounds %main.hasPadding, ptr %hashmap.key, i32 1
149149
call void @runtime.memzero(ptr nonnull %1, i32 3, ptr undef) #4
150150
%2 = getelementptr inbounds [2 x %main.hasPadding], ptr %hashmap.key, i32 0, i32 1, i32 1
151151
call void @runtime.memzero(ptr nonnull %2, i32 3, ptr undef) #4
152-
%3 = getelementptr inbounds i8, ptr %hashmap.key, i32 21
152+
%3 = getelementptr inbounds [2 x %main.hasPadding], ptr %hashmap.key, i32 1
153153
call void @runtime.memzero(ptr nonnull %3, i32 3, ptr undef) #4
154154
call void @runtime.hashmapBinarySet(ptr %m, ptr nonnull %hashmap.key, ptr nonnull %hashmap.value, ptr undef) #4
155155
call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %hashmap.key)

0 commit comments

Comments
 (0)