summaryrefslogtreecommitdiff
path: root/src/ToolBox/superpmi/mcs/verbremovedup.cpp
blob: eca5dc1b09cff9826fd3948f6fe61ab5df572411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

#include "standardpch.h"
#include "verbremovedup.h"
#include "simpletimer.h"
#include "lightweightmap.h"
#include "methodcontext.h"
#include "methodcontextiterator.h"

//We use a hash to limit the number of comparisons we need to do.
//The first level key to our hash map is ILCodeSize and the second
//level map key is just an index and the value is an existing MC Hash.

LightWeightMap<int, DenseLightWeightMap<char *> *> *inFile = nullptr;

bool unique(MethodContext *mc)
{
    if (inFile == nullptr)
        inFile = new LightWeightMap<int, DenseLightWeightMap<char *> *>();

    CORINFO_METHOD_INFO newInfo;
    unsigned newFlags = 0;
    mc->repCompileMethod(&newInfo, &newFlags);

    char *md5Buff = new char[MD5_HASH_BUFFER_SIZE];
    mc->dumpMethodMD5HashToBuffer(md5Buff, MD5_HASH_BUFFER_SIZE);

    if (inFile->GetIndex(newInfo.ILCodeSize) == -1)
        inFile->Add(newInfo.ILCodeSize, new DenseLightWeightMap<char *>());

    DenseLightWeightMap<char *> *ourRank = inFile->Get(newInfo.ILCodeSize);

    for (int i = 0; i < (int)ourRank->GetCount(); i++)
    {
        char *md5Buff2 = ourRank->Get(i);

        if (strncmp(md5Buff, md5Buff2, MD5_HASH_BUFFER_SIZE) == 0)
        {
            delete[] md5Buff;
            return false;
        }
    }

    ourRank->Append(md5Buff);
    return true;
}

LightWeightMap<int, DenseLightWeightMap<MethodContext *> *> *inFileLegacy = nullptr;

bool uniqueLegacy(MethodContext *mc)
{
    if (inFileLegacy == nullptr)
        inFileLegacy = new LightWeightMap<int, DenseLightWeightMap<MethodContext *> *>();

    CORINFO_METHOD_INFO newInfo;
    unsigned newFlags = 0;
    mc->repCompileMethod(&newInfo, &newFlags);

    if (inFileLegacy->GetIndex(newInfo.ILCodeSize) == -1)
        inFileLegacy->Add(newInfo.ILCodeSize, new DenseLightWeightMap<MethodContext *>());

    DenseLightWeightMap<MethodContext *> *ourRank = inFileLegacy->Get(newInfo.ILCodeSize);

    for (int i = 0; i < (int)ourRank->GetCount(); i++)
    {
        MethodContext *scratch = ourRank->Get(i);
        if (mc->Equal(scratch))
        {
            return false;
        }
    }

    // We store the MethodContext in our map.
    ourRank->Append(mc);
    return true;
}

int verbRemoveDup::DoWork(const char *nameOfInput, const char *nameOfOutput, bool stripCR, bool legacyCompare)
{
    LogVerbose("Removing duplicates from '%s', writing to '%s'", nameOfInput, nameOfOutput);

    MethodContextIterator mci(true);
    if (!mci.Initialize(nameOfInput))
        return -1;

    int savedCount = 0;

    HANDLE hFileOut = CreateFileA(nameOfOutput, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if (hFileOut == INVALID_HANDLE_VALUE)
    {
        LogError("Failed to open output '%s'. GetLastError()=%u", nameOfOutput, GetLastError());
        return -1;
    }

    while (mci.MoveNext())
    {
        MethodContext* mc = mci.CurrentTakeOwnership();
        if (stripCR)
        {
            delete mc->cr;
            mc->cr = new CompileResult();
        }
        if (legacyCompare)
        {
            if (uniqueLegacy(mc))
            {
                mc->saveToFile(hFileOut);
                savedCount++;

                // In this case, for the legacy comparer, it has placed the 'mc' in the 'inFileLegacy' table, so we can't delete it.
            }
            else
            {
                delete mc; // we no longer need this
            }
        }
        else
        {
            if (unique(mc))
            {
                mc->saveToFile(hFileOut);
                savedCount++;
            }
            delete mc; // we no longer need this
        }
    }

    // We're leaking 'inFile' or 'inFileLegacy', but the process is going away, so it shouldn't matter.

    if (CloseHandle(hFileOut) == 0)
    {
        LogError("2nd CloseHandle failed. GetLastError()=%u", GetLastError());
        return -1;
    }

    LogInfo("Loaded %d, Saved %d", mci.MethodContextNumber(), savedCount);

    if (!mci.Destroy())
        return -1;

    return 0;
}