Skip to content
Permalink
Browse files

[AOT] fix cross platform issue: using uint64_t to replace size_t. (#2626

)

Description: Ahead-of-time Compilation cross platform issue: build on 64-bit OS and run on 32-bit OS.
Testing: x86_64 and armv7 platform.
Documentation: modify AOT.md

Fixes #2592
  • Loading branch information...
xizhizhang authored and jfix71 committed Apr 4, 2019
1 parent a5d1835 commit d4fbe129de01efa7326a4a9a5b5e8c119c4f7306
Showing with 47 additions and 44 deletions.
  1. +7 −7 docs/AOT.md
  2. +7 −7 examples/bundles/lenet_mnist/main.cpp
  3. +8 −7 examples/bundles/resnet50/main.cpp
  4. +25 −23 lib/LLVMIRCodeGen/BundleSaver.cpp
@@ -87,15 +87,15 @@ the compiled network. The type of this symbol is always the following struct:
```c++
struct BundleConfig {
// Size of the constant weight variables memory area.
size_t constantWeightVarsMemSize;
uint64_t constantWeightVarsMemSize;
// Size of the mutable weight variables memory area.
size_t mutableWeightVarsMemSize;
uint64_t mutableWeightVarsMemSize;
// Size of the activations memory area.
size_t activationsMemSize;
uint64_t activationsMemSize;
// Alignment to be used for weights and activations.
size_t alignment;
uint64_t alignment;
// Number of symbols in the symbol table.
size_t numSymbols;
uint64_t numSymbols;
// Symbol table.
const SymbolTableEntry *symbolTable;
};
@@ -112,9 +112,9 @@ struct SymbolTableEntry {
// Name of a variable.
const char *name;
// Offset of the variable inside the memory area.
size_t offset;
uint64_t offset;
// The number of elements inside this variable.
size_t size;
uint64_t size;
// The kind of the variable. 1 if it is a mutable variable, 0 otherwise.
char kind;
};
@@ -202,25 +202,25 @@ struct SymbolTableEntry {
// Name of a variable.
const char *name;
// Offset of the variable inside the memory area.
size_t offset;
uint64_t offset;
// The number of elements inside this variable.
size_t size;
uint64_t size;
// The kind of the variable. 1 if it is a mutable variable, 0 otherwise.
char kind;
};

/// Type describing the config of a generated bundle.
struct BundleConfig {
// Size of the constant weight variables memory area.
size_t constantWeightVarsMemSize;
uint64_t constantWeightVarsMemSize;
// Size of the mutable weight variables memory area.
size_t mutableWeightVarsMemSize;
uint64_t mutableWeightVarsMemSize;
// Size of the activations memory area.
size_t activationsMemSize;
uint64_t activationsMemSize;
// Alignment to be used for weights and activations.
size_t alignment;
uint64_t alignment;
// Number of symbols in the symbol table.
size_t numSymbols;
uint64_t numSymbols;
// Symbol table.
const SymbolTableEntry *symbolTable;
};
@@ -17,6 +17,7 @@
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <string>
#include <vector>
@@ -216,25 +217,25 @@ struct SymbolTableEntry {
// Name of a variable.
const char *name;
// Offset of the variable inside the memory area.
size_t offset;
uint64_t offset;
// The number of elements inside this variable.
size_t size;
uint64_t size;
// The kind of the variable. 1 if it is a mutable variable, 0 otherwise.
char kind;
};

/// Type describing the config of a generated bundle.
struct BundleConfig {
// Size of the constant weight variables memory area.
size_t constantWeightVarsMemSize;
uint64_t constantWeightVarsMemSize;
// Size of the mutable weight variables memory area.
size_t mutableWeightVarsMemSize;
uint64_t mutableWeightVarsMemSize;
// Size of the activations memory area.
size_t activationsMemSize;
uint64_t activationsMemSize;
// Alignment to be used for weights and activations.
size_t alignment;
uint64_t alignment;
// Number of symbols in the symbol table.
size_t numSymbols;
uint64_t numSymbols;
// Symbol table.
const SymbolTableEntry *symbolTable;
};
@@ -82,16 +82,16 @@ void BundleSaver::emitSymbolTable() {
// Define a struct for symbol table entries:
// struct SymbolTableEntry {
// const char *name;
// size_t offset;
// size_t size;
// uint64_t offset;
// uint64_t size;
// char kind;
// };
auto *charTy = llvm::Type::getInt8Ty(irgen_->getLLVMContext());
auto *sizeTTy =
llvm::Type::getIntNTy(irgen_->getLLVMContext(), sizeof(size_t) * 8);
auto symbolTableEntryTy =
llvm::StructType::get(irgen_->getLLVMContext(),
{charTy->getPointerTo(), sizeTTy, sizeTTy, charTy});
auto *uint64TTy =
llvm::Type::getIntNTy(irgen_->getLLVMContext(), sizeof(uint64_t) * 8);
auto symbolTableEntryTy = llvm::StructType::get(
irgen_->getLLVMContext(),
{charTy->getPointerTo(), uint64TTy, uint64TTy, charTy});
// Set of entries in the symbol table.
llvm::SmallVector<llvm::Constant *, 128> entries;
// Iterate over all Placeholders and record information about their names,
@@ -108,9 +108,9 @@ void BundleSaver::emitSymbolTable() {
irgen_->emitStringConst(irgen_->getBuilder(), w->getName()),
charTy->getPointerTo())),
// offset.
llvm::ConstantInt::get(sizeTTy, addr),
llvm::ConstantInt::get(uint64TTy, addr),
// size.
llvm::ConstantInt::get(sizeTTy, size),
llvm::ConstantInt::get(uint64TTy, size),
// 1 for Mutable Kind
llvm::ConstantInt::get(charTy, 1)});
entries.push_back(entry);
@@ -219,39 +219,41 @@ void BundleSaver::emitBundleEntryFunction() {
// so that they know how much memory they need to allocate, etc.
// Config consists of the following fields:
// struct BundleConfig {
// size_t constantWeightVarsMemSize;
// size_t mutableWeightVarsMemSize;
// size_t activationsMemSize;
// size_t alignment;
// size_t numSymbols;
// uint64_t constantWeightVarsMemSize;
// uint64_t mutableWeightVarsMemSize;
// uint64_t activationsMemSize;
// uint64_t alignment;
// uint64_t numSymbols;
// SymbolTableEntry *symbolTable;
// };
void BundleSaver::emitBundleConfig() {
auto symbolTable = irgen_->getModule().getGlobalVariable(
irgen_->getMainEntryName() + "SymbolTable", true);
GLOW_ASSERT(symbolTable &&
"Expected to find a symbol table for the AOT bundle");
// Get the integer type having the same size in bits as size_t.
auto *SizeTType = irgen_->getBuilder().getIntNTy(sizeof(size_t) * 8);
// Get the integer type having the same size in bits as uint64_t.
auto *uint64TType = irgen_->getBuilder().getIntNTy(sizeof(uint64_t) * 8);
auto symbolTableEntryTy = symbolTable->getType()->getPointerElementType();
auto *bundleConfigTy =
llvm::StructType::get(irgen_->getLLVMContext(),
{SizeTType, SizeTType, SizeTType, SizeTType,
SizeTType, symbolTableEntryTy->getPointerTo()});
{uint64TType, uint64TType, uint64TType, uint64TType,
uint64TType, symbolTableEntryTy->getPointerTo()});
auto config = new llvm::GlobalVariable(
irgen_->getModule(), bundleConfigTy, /* isConst */ true,
llvm::GlobalValue::LinkageTypes::ExternalLinkage, nullptr,
irgen_->getMainEntryName() + "_config");
config->setInitializer(llvm::ConstantStruct::get(
bundleConfigTy,
llvm::ConstantInt::get(
SizeTType, irgen_->getAllocationsInfo().constantWeightVarsMemSize_),
uint64TType, irgen_->getAllocationsInfo().constantWeightVarsMemSize_),
llvm::ConstantInt::get(
SizeTType, irgen_->getAllocationsInfo().mutableWeightVarsMemSize_),
llvm::ConstantInt::get(SizeTType,
uint64TType, irgen_->getAllocationsInfo().mutableWeightVarsMemSize_),
llvm::ConstantInt::get(uint64TType,
irgen_->getAllocationsInfo().activationsMemSize_),
llvm::ConstantInt::get(SizeTType, TensorAlignment),
llvm::ConstantInt::get(SizeTType, F_->findConstants().size()),

llvm::ConstantInt::get(uint64TType, TensorAlignment),
llvm::ConstantInt::get(uint64TType, F_->findConstants().size()),

symbolTable));
}

0 comments on commit d4fbe12

Please sign in to comment.
You can’t perform that action at this time.