From 972043c146bc17a2f7059108200f6223c4208b76 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Mon, 9 Apr 2012 14:23:08 -0700 Subject: [PATCH] Fix serious bug in handling constant-valued initializers. In InitSymbol(), we try to be smart and emit a memcpy when there are a number of values to store (e.g. for arrays, structs, etc.) Unfortunately, this wasn't working as desired for bools (i.e. i1 types), since the SizeOf() call that tried to figure out how many bytes to copy would return 0 bytes, due to dividing the number of bits to copy by 8. Fixes issue #234. --- expr.cpp | 18 +++++++++++------- ispc.cpp | 4 +++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/expr.cpp b/expr.cpp index 81f51151..2ad7a495 100644 --- a/expr.cpp +++ b/expr.cpp @@ -643,13 +643,17 @@ InitSymbol(llvm::Value *ptr, const Type *symType, Expr *initExpr, return; } - llvm::Value *constPtr = - new llvm::GlobalVariable(*m->module, llvmType, true /* const */, - llvm::GlobalValue::InternalLinkage, - constValue, "const_initializer"); - llvm::Value *size = g->target.SizeOf(llvmType, - ctx->GetCurrentBasicBlock()); - ctx->MemcpyInst(ptr, constPtr, size); + if (Type::IsBasicType(symType)) + ctx->StoreInst(constValue, ptr); + else { + llvm::Value *constPtr = + new llvm::GlobalVariable(*m->module, llvmType, true /* const */, + llvm::GlobalValue::InternalLinkage, + constValue, "const_initializer"); + llvm::Value *size = g->target.SizeOf(llvmType, + ctx->GetCurrentBasicBlock()); + ctx->MemcpyInst(ptr, constPtr, size); + } return; } diff --git a/ispc.cpp b/ispc.cpp index 4e39c0b2..e9357832 100644 --- a/ispc.cpp +++ b/ispc.cpp @@ -518,7 +518,9 @@ Target::SizeOf(LLVM_TYPE_CONST llvm::Type *type, const llvm::TargetData *td = GetTargetMachine()->getTargetData(); Assert(td != NULL); - uint64_t byteSize = td->getTypeSizeInBits(type) / 8; + uint64_t bitSize = td->getTypeSizeInBits(type); + Assert((bitSize % 8) == 0); + uint64_t byteSize = bitSize / 8; if (is32Bit || g->opt.force32BitAddressing) return LLVMInt32((int32_t)byteSize); else