41
41
#include " llvm/InitializePasses.h"
42
42
#include " llvm/Pass.h"
43
43
#include " llvm/Support/Casting.h"
44
- #include " llvm/Support/CommandLine.h"
45
44
#include " llvm/Support/Debug.h"
46
45
#include " llvm/Support/raw_ostream.h"
47
46
#include " llvm/Transforms/IPO.h"
@@ -55,11 +54,6 @@ using namespace llvm;
55
54
56
55
#define DEBUG_TYPE " deadargelim"
57
56
58
- static cl::opt<std::string>
59
- IntegrationHeaderFileName (" integr-header-file" ,
60
- cl::desc (" Path to integration header file" ),
61
- cl::value_desc(" filename" ), cl::Hidden);
62
-
63
57
STATISTIC (NumArgumentsEliminated, " Number of unread args removed" );
64
58
STATISTIC (NumRetValsEliminated , " Number of unused return values removed" );
65
59
STATISTIC (NumArgumentsReplacedWithUndef,
@@ -760,78 +754,6 @@ void DeadArgumentEliminationPass::PropagateLiveness(const RetOrArg &RA) {
760
754
Uses.erase (Begin, I);
761
755
}
762
756
763
- // Update kernel arguments table inside the integration header.
764
- // For example:
765
- // static constexpr const bool param_omit_table[] = {
766
- // // OMIT_TABLE_BEGIN
767
- // // kernel_name_1
768
- // false, false, // <= update to true if the argument is dead
769
- // // kernel_name_2
770
- // false, false,
771
- // // OMIT_TABLE_END
772
- // };
773
- // TODO: batch changes to multiple SPIR kernels and do one bulk update.
774
- constexpr StringLiteral OMIT_TABLE_BEGIN (" // OMIT_TABLE_BEGIN" );
775
- constexpr StringLiteral OMIT_TABLE_END (" // OMIT_TABLE_END" );
776
- static void updateIntegrationHeader (StringRef SpirKernelName,
777
- const ArrayRef<bool > &ArgAlive) {
778
- ErrorOr<std::unique_ptr<MemoryBuffer>> IntHeaderBuffer =
779
- MemoryBuffer::getFile (IntegrationHeaderFileName);
780
-
781
- if (!IntHeaderBuffer)
782
- report_fatal_error (" unable to read integration header file '" +
783
- IntegrationHeaderFileName +
784
- " ': " + IntHeaderBuffer.getError ().message ());
785
-
786
- // 1. Find the region between OMIT_TABLE_BEGIN and OMIT_TABLE_END
787
- StringRef IntHeader ((*IntHeaderBuffer)->getBuffer ());
788
- if (!IntHeader.contains (OMIT_TABLE_BEGIN))
789
- report_fatal_error (OMIT_TABLE_BEGIN +
790
- " marker not found in integration header" );
791
- if (!IntHeader.contains (OMIT_TABLE_END))
792
- report_fatal_error (OMIT_TABLE_END +
793
- " marker not found in integration header" );
794
-
795
- size_t BeginRegionPos =
796
- IntHeader.find (OMIT_TABLE_BEGIN) + OMIT_TABLE_BEGIN.size ();
797
- size_t EndRegionPos = IntHeader.find (OMIT_TABLE_END);
798
-
799
- StringRef OmitArgTable = IntHeader.slice (BeginRegionPos, EndRegionPos);
800
-
801
- // 2. Find the line that corresponds to the SPIR kernel
802
- if (!OmitArgTable.contains (SpirKernelName))
803
- report_fatal_error (
804
- " Argument table not found in integration header for function '" +
805
- SpirKernelName + " '" );
806
-
807
- size_t BeginLinePos =
808
- OmitArgTable.find (SpirKernelName) + SpirKernelName.size ();
809
- size_t EndLinePos = OmitArgTable.find (" //" , BeginLinePos);
810
-
811
- StringRef OmitArgLine = OmitArgTable.slice (BeginLinePos, EndLinePos);
812
-
813
- size_t LineLeftTrim = OmitArgLine.size () - OmitArgLine.ltrim ().size ();
814
- size_t LineRightTrim = OmitArgLine.size () - OmitArgLine.rtrim ().size ();
815
-
816
- // 3. Construct new file contents and replace only that string.
817
- std::string NewIntHeader;
818
- NewIntHeader +=
819
- IntHeader.take_front (BeginRegionPos + BeginLinePos + LineLeftTrim);
820
- for (auto &AliveArg : ArgAlive)
821
- NewIntHeader += AliveArg ? " false, " : " true, " ;
822
- NewIntHeader += IntHeader.drop_front (BeginRegionPos + BeginLinePos +
823
- OmitArgLine.size () - LineRightTrim);
824
-
825
- // 4. Flush the string into the file.
826
- std::error_code EC;
827
- raw_fd_ostream File (IntegrationHeaderFileName, EC, sys::fs::F_Text);
828
-
829
- if (EC)
830
- report_fatal_error (" Cannot open integration header for writing." );
831
-
832
- File << NewIntHeader;
833
- }
834
-
835
757
// RemoveDeadStuffFromFunction - Remove any arguments and return values from F
836
758
// that are not in LiveValues. Transform the function and all of the callees of
837
759
// the function to not have these arguments and return values.
@@ -875,8 +797,17 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {
875
797
}
876
798
}
877
799
878
- if (CheckSpirKernels)
879
- updateIntegrationHeader (F->getName (), ArgAlive);
800
+ if (CheckSpirKernels) {
801
+ SmallVector<Metadata *, 10 > MDOmitArgs;
802
+ auto MDOmitArgTrue = llvm::ConstantAsMetadata::get (
803
+ ConstantInt::get (Type::getInt1Ty (F->getContext ()), 1 ));
804
+ auto MDOmitArgFalse = llvm::ConstantAsMetadata::get (
805
+ ConstantInt::get (Type::getInt1Ty (F->getContext ()), 0 ));
806
+ for (auto &AliveArg : ArgAlive)
807
+ MDOmitArgs.push_back (AliveArg ? MDOmitArgFalse : MDOmitArgTrue);
808
+ F->setMetadata (" spir_kernel_omit_args" ,
809
+ llvm::MDNode::get (F->getContext (), MDOmitArgs));
810
+ }
880
811
881
812
// Find out the new return value.
882
813
Type *RetTy = FTy->getReturnType ();
@@ -1193,11 +1124,6 @@ bool DeadArgumentEliminationPass::RemoveDeadStuffFromFunction(Function *F) {
1193
1124
1194
1125
PreservedAnalyses DeadArgumentEliminationPass::run (Module &M,
1195
1126
ModuleAnalysisManager &) {
1196
- // Integration header file must be provided for
1197
- // DAE to work on SPIR kernels.
1198
- if (CheckSpirKernels && !IntegrationHeaderFileName.getNumOccurrences ())
1199
- return PreservedAnalyses::all ();
1200
-
1201
1127
bool Changed = false ;
1202
1128
1203
1129
// First pass: Do a simple check to see if any functions can have their "..."
0 commit comments