Skip to content

Commit

Permalink
upload memory alignment manually
Browse files Browse the repository at this point in the history
Addresses #644.
  • Loading branch information
PeiMu committed Mar 5, 2023
1 parent 50853c4 commit 26ebf8e
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 13 deletions.
46 changes: 46 additions & 0 deletions analysis/statistics/37f066bbf917f28ca09d3d6cbe00ac3452311685.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

changeset: 1440:37f066bbf917f28ca09d3d6cbe00ac3452311685
char kNewtonVersion[] = "0.3-alpha-1440 (37f066bbf917f28ca09d3d6cbe00ac3452311685) (build 03-05-2023-13:[email protected]_64)";
\n./src/noisy/noisy-linux-EN -O0 applications/noisy/helloWorld.n -s
\n./src/newton/newton-linux-EN -v 0 -eP applications/newton/invariants/ViolinWithTemperatureDependence-pigroups.nt

Informational Report:
---------------------
Invariant "ViolinWithTemperatureDependenceForPiGroups" has 2 unique kernels, each with 2 column(s)...

Kernel 0 is a valid kernel:

1 1
-0.5 -0
1 0
0.5 0
0 -1
-0 -1


The ordering of parameters is: P1 P0 P3 P2 P4 P5

Pi group 0, Pi 0 is: P0^(-0.5) P1^( 1) P2^(0.5) P3^( 1) P4^( 0) P5^(-0)

Pi group 0, Pi 1 is: P0^(-0) P1^( 1) P2^( 0) P3^( 0) P4^(-1) P5^(-1)


Kernel 1 is a valid kernel:

1 0
-0.5 1
1 -2
0.5 -1
-0 -2
0 -2


The ordering of parameters is: P1 P0 P3 P2 P4 P5

Pi group 1, Pi 0 is: P0^(-0.5) P1^( 1) P2^(0.5) P3^( 1) P4^(-0) P5^( 0)

Pi group 1, Pi 1 is: P0^( 1) P1^( 0) P2^(-1) P3^(-2) P4^(-2) P5^(-2)




8 changes: 8 additions & 0 deletions src/newton/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ SOURCES =\
newton-irPass-LLVMIR-constantSubstitution.cpp\
newton-irPass-LLVMIR-shrinkTypeByRange.cpp\
newton-irPass-LLVMIR-quantization.cpp\
newton-irPass-LLVMIR-memoryAlignment.cpp\


#
Expand Down Expand Up @@ -151,6 +152,7 @@ OBJS =\
newton-ffi2code-autoGeneratedSets.$(OBJECTEXTENSION)\
newton-eigenLibraryInterface.$(OBJECTEXTENSION)\
newton-irPass-targetParamBackend.$(OBJECTEXTENSION)\
newton-irPass-LLVMIR-memoryAlignment.$(OBJECTEXTENSION)\


CGIOBJS =\
Expand Down Expand Up @@ -199,6 +201,7 @@ CGIOBJS =\
newton-ffi2code-autoGeneratedSets.$(OBJECTEXTENSION)\
newton-eigenLibraryInterface.$(OBJECTEXTENSION)\
newton-irPass-targetParamBackend.$(OBJECTEXTENSION)\
newton-irPass-LLVMIR-memoryAlignment.$(OBJECTEXTENSION)\


LIBNEWTONOBJS =\
Expand Down Expand Up @@ -244,6 +247,7 @@ LIBNEWTONOBJS =\
newton-ffi2code-autoGeneratedSets.$(OBJECTEXTENSION)\
newton-eigenLibraryInterface.$(OBJECTEXTENSION)\
newton-irPass-targetParamBackend.$(OBJECTEXTENSION)\
newton-irPass-LLVMIR-memoryAlignment.$(OBJECTEXTENSION)\


HEADERS =\
Expand Down Expand Up @@ -361,6 +365,10 @@ newton-irPass-LLVMIR-quantization.$(OBJECTEXTENSION): newton-irPass-LLVMIR-quant
$(CXX) $(FLEXFLAGS) $(INCDIRS) $(CXXFLAGS) $(WFLAGS) $(OPTFLAGS) $(LINTFLAGS) $<
$(CXX) $(FLEXFLAGS) $(INCDIRS) $(CXXFLAGS) $(WFLAGS) $(OPTFLAGS) $<

newton-irPass-LLVMIR-memoryAlignment.$(OBJECTEXTENSION): newton-irPass-LLVMIR-memoryAlignment.cpp
$(CXX) $(FLEXFLAGS) $(INCDIRS) $(CXXFLAGS) $(WFLAGS) $(OPTFLAGS) $(LINTFLAGS) $<
$(CXX) $(FLEXFLAGS) $(INCDIRS) $(CXXFLAGS) $(WFLAGS) $(OPTFLAGS) $<

version.c: $(HEADERS) Makefile
echo 'char kNewtonVersion[] = "0.3-alpha-'`git rev-list --count HEAD`' ('`git rev-parse HEAD`') (build '`date '+%m-%d-%Y-%H:%M'`-`whoami`@`hostname -s`-`uname -s`-`uname -r`-`uname -m`\)\"\; > version.c

Expand Down
188 changes: 188 additions & 0 deletions src/newton/newton-irPass-LLVMIR-memoryAlignment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
//
// Created by stephen on 15/02/23.
//

/*
Authored 2022. Stephen Huang.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

#include "newton-irPass-LLVMIR-memoryAlignment.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/DerivedTypes.h"

using namespace llvm;

extern "C"
{
/*
* Steps of constantSubstitution:
* 1. for each instruction (that is the case statement), get the range of current instruction from boundInfo
* 2. check if the lower range and upper range is the same value, then it means this is a constant value instruction
* 3. get the type of current constant value instruction, mainly float/double/integer (with different bits)
* 4. use llvm API to create a new constant value
* 5. substitute current instruction with the constant value
* */

void
memoryAlignment(State * N, BoundInfo * boundInfo, llvm::Function & llvmIrFunction)
{
/*
* Some special instructions that need to pay attention:
* %i = alloca type, the type of this instruction is "type*"
* %i = call retType @func_name (type %p1, ...)
* call void @llvm.dbg.declare/value (metadata type %p, ...)
* %i = load type, type* %op, the type of this instruction is "type"
* %i = gep type, type1* %op1, type2 %op2, (type3 %op3)
* %i = castInst type1 %op1 to type2
* store type %op1, type* %op2
* %.i = phi type [%op1, %bb1], [%op2, %bb2], ...
* %i = binary type %op1, %op2
* %i = unary type %op
* */
// printf("<<<<<<<<<< Memeory Alignment >>>>>>>>>>\n\n");
llvm::Module *module = llvmIrFunction.getParent();
auto dataLayout = module->getDataLayout();

// llvmIrFunction.print(llvm::outs());
// printf("\n");
for (BasicBlock & llvmIrBasicBlock : llvmIrFunction)
{
for (BasicBlock::iterator itBB = llvmIrBasicBlock.begin(); itBB != llvmIrBasicBlock.end();)
{
Instruction * llvmIrInstruction = &*itBB++;
switch (llvmIrInstruction->getOpcode())
{
// case Instruction::CmpXchg:
// case Instruction::Va_Arg:
// case Instruction::Phi:
case Instruction::Store:
{
// printf("\n> Load\n");
// cast the general llvm instruction to a specific instruction
llvm::StoreInst* storeInst = llvm::dyn_cast<llvm::StoreInst>(llvmIrInstruction);

if (storeInst)
{
llvm::Value *storedValue = storeInst->getValueOperand();
llvm::Type *resultType = storedValue->getType();

unsigned align = storeInst->getAlignment();
// if align > 0, that means no align parameter
if(align > 0)
{
// The result type could not be a void type
if (!resultType->isVoidTy())
{
unsigned resultAlignment = dataLayout.getABITypeAlignment(resultType);

// if original alignment is not equal to the result alignment, that means it is not correctly aligned
if (resultAlignment != align)
{
// reset the alignment of the instruction
storeInst->setAlignment(llvm::Align(resultAlignment));
}
}

}
}

break;

}
case Instruction::Load:
{
// printf("\n> Load\n");
auto vrIt = boundInfo->virtualRegisterRange.find(llvmIrInstruction);
if (vrIt == boundInfo->virtualRegisterRange.end())
{
// printf(">> load break!\n");
break;
}

if(llvmIrInstruction->hasMetadata()){
// printf(">>> Has MetaData!\n");

// cast the general llvm instruction to a specific instruction
llvm::LoadInst* loadInstr = llvm::dyn_cast<llvm::LoadInst>(llvmIrInstruction);
if (loadInstr)
{
unsigned align = loadInstr->getAlignment();
llvm::Type * resultType = loadInstr->getType();

if(align > 0 && !resultType->isVoidTy())
{
unsigned resultAlignment = dataLayout.getABITypeAlignment(resultType);

// if original alignment is not equal to the result alignment, that means it is not correctly aligned
if (resultAlignment != align)
{
// reset the alignment of the instruction
loadInstr->setAlignment(llvm::Align(resultAlignment));
}
}

}
break;

}
}

case Instruction::Alloca:
{
llvmIrInstruction->print(llvm::outs());
llvm::AllocaInst* allocaInst = llvm::dyn_cast<llvm::AllocaInst>(llvmIrInstruction);
llvm::Type *type = allocaInst->getAllocatedType();
if (isa<ArrayType>(type)){
break;
}
else if(isa<StructType>(type)){
StructType *strucTy = dyn_cast<StructType>(type);
unsigned alignment = dataLayout.getABITypeAlignment(strucTy);
allocaInst->setAlignment(llvm::Align(alignment));
}
break;
}

default:
break;

}
}
}
}
}




18 changes: 18 additions & 0 deletions src/newton/newton-irPass-LLVMIR-memoryAlignment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Created by stephen on 15/02/23.
//


#include "newton-irPass-LLVMIR-rangeAnalysis.h"
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

void
memoryAlignment(State * N, BoundInfo * boundInfo, llvm::Function & llvmIrFunction);

#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */

40 changes: 27 additions & 13 deletions src/newton/newton-irPass-LLVMIR-optimizeByRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "newton-irPass-LLVMIR-constantSubstitution.h"
#include "newton-irPass-LLVMIR-shrinkTypeByRange.h"
#include "newton-irPass-LLVMIR-quantization.h"
#include "newton-irPass-LLVMIR-memoryAlignment.h"
#endif /* __cplusplus */

#include <algorithm>
Expand Down Expand Up @@ -387,16 +388,18 @@ irPassLLVMIROptimizeByRange(State * N)
// }
}

// flexprint(N->Fe, N->Fm, N->Fpinfo, "shrink data type by range\n");
// for (auto & mi : *Mod)
// {
// auto boundInfoIt = funcBoundInfo.find(mi.getName().str());
// if (boundInfoIt != funcBoundInfo.end()) {
// shrinkType(N, boundInfoIt->second, mi);
// } else {
// assert(false);
// }
// }
flexprint(N->Fe, N->Fm, N->Fpinfo, "shrink data type by range\n");
for (auto & mi : *Mod)
{
auto boundInfoIt = funcBoundInfo.find(mi.getName().str());
if (boundInfoIt != funcBoundInfo.end()) {
shrinkType(N, boundInfoIt->second, mi);
}
// else
// {
// assert(false);
// }
}

/*
* remove the functions that are optimized by passes.
Expand All @@ -407,6 +410,20 @@ irPassLLVMIROptimizeByRange(State * N)
if (useOverLoad)
overloadFunc(Mod, callerMap);

flexprint(N->Fe, N->Fm, N->Fpinfo, "memory alignment\n");
for (auto & mi : *Mod)
{
auto boundInfoIt = funcBoundInfo.find(mi.getName().str());
if (boundInfoIt != funcBoundInfo.end())
{
memoryAlignment(N, boundInfoIt->second, mi);
}
// else
// {
// assert(false);
// }
}

flexprint(N->Fe, N->Fm, N->Fpinfo, "infer bound\n");
funcBoundInfo.clear();
for (auto & mi : *Mod)
Expand Down Expand Up @@ -436,9 +453,6 @@ irPassLLVMIROptimizeByRange(State * N)
// }
}

// passManager.add(createGlobalDCEPass());
// passManager.run(*Mod);

/*
* remove the functions that are optimized by passes.
* */
Expand Down

0 comments on commit 26ebf8e

Please sign in to comment.