blob: 3cfde5a55b7cde93f33f05977659396306844497 [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001//===------------------------- LSUnit.h --------------------------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
11/// A Load/Store unit class that models load/store queues and that implements
12/// a simple weak memory consistency model.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_TOOLS_LLVM_MCA_LSUNIT_H
17#define LLVM_TOOLS_LLVM_MCA_LSUNIT_H
18
19#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
21#include <set>
22
23#define DEBUG_TYPE "llvm-mca"
24
25namespace mca {
26
Andrea Di Biagio2dee62b2018-03-22 14:14:49 +000027struct InstrDesc;
28
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000029/// \brief A Load/Store Unit implementing a load and store queues.
30///
31/// This class implements a load queue and a store queue to emulate the
32/// out-of-order execution of memory operations.
33/// Each load (or store) consumes an entry in the load (or store) queue.
34///
35/// Rules are:
36/// 1) A younger load is allowed to pass an older load only if there are no
37/// stores nor barriers in between the two loads.
38/// 2) An younger store is not allowed to pass an older store.
39/// 3) A younger store is not allowed to pass an older load.
40/// 4) A younger load is allowed to pass an older store only if the load does
41/// not alias with the store.
42///
43/// This class optimistically assumes that loads don't alias store operations.
44/// Under this assumption, younger loads are always allowed to pass older
45/// stores (this would only affects rule 4).
46/// Essentially, this LSUnit doesn't attempt to run any sort alias analysis to
47/// predict when loads and stores don't alias with eachother.
48///
49/// To enforce aliasing between loads and stores, flag `AssumeNoAlias` must be
50/// set to `false` by the constructor of LSUnit.
51///
52/// In the case of write-combining memory, rule 2. could be relaxed to allow
53/// reordering of non-aliasing store operations. At the moment, this is not
54/// allowed.
55/// To put it in another way, there is no option to specify a different memory
56/// type for memory operations (example: write-through, write-combining, etc.).
57/// Also, there is no way to weaken the memory model, and this unit currently
58/// doesn't support write-combining behavior.
59///
60/// No assumptions are made on the size of the store buffer.
61/// As mentioned before, this class doesn't perform alias analysis.
62/// Consequently, LSUnit doesn't know how to identify cases where
63/// store-to-load forwarding may occur.
64///
65/// LSUnit doesn't attempt to predict whether a load or store hits or misses
66/// the L1 cache. To be more specific, LSUnit doesn't know anything about
67/// the cache hierarchy and memory types.
68/// It only knows if an instruction "mayLoad" and/or "mayStore". For loads, the
69/// scheduling model provides an "optimistic" load-to-use latency (which usually
70/// matches the load-to-use latency for when there is a hit in the L1D).
71///
72/// Class MCInstrDesc in LLVM doesn't know about serializing operations, nor
73/// memory-barrier like instructions.
74/// LSUnit conservatively assumes that an instruction which `mayLoad` and has
75/// `unmodeled side effects` behave like a "soft" load-barrier. That means, it
76/// serializes loads without forcing a flush of the load queue.
77/// Similarly, instructions that both `mayStore` and have `unmodeled side
78/// effects` are treated like store barriers. A full memory
79/// barrier is a 'mayLoad' and 'mayStore' instruction with unmodeled side
80/// effects. This is obviously inaccurate, but this is the best that we can do
81/// at the moment.
82///
83/// Each load/store barrier consumes one entry in the load/store queue. A
84/// load/store barrier enforces ordering of loads/stores:
85/// - A younger load cannot pass a load barrier.
86/// - A younger store cannot pass a store barrier.
87///
88/// A younger load has to wait for the memory load barrier to execute.
89/// A load/store barrier is "executed" when it becomes the oldest entry in
90/// the load/store queue(s). That also means, all the older loads/stores have
91/// already been executed.
92class LSUnit {
93 // Load queue size.
94 // LQ_Size == 0 means that there are infinite slots in the load queue.
95 unsigned LQ_Size;
96
97 // Store queue size.
98 // SQ_Size == 0 means that there are infinite slots in the store queue.
99 unsigned SQ_Size;
100
101 // If true, loads will never alias with stores. This is the default.
102 bool NoAlias;
103
104 std::set<unsigned> LoadQueue;
105 std::set<unsigned> StoreQueue;
106
107 void assignLQSlot(unsigned Index);
108 void assignSQSlot(unsigned Index);
109 bool isReadyNoAlias(unsigned Index) const;
110
111 // An instruction that both 'mayStore' and 'HasUnmodeledSideEffects' is
112 // conservatively treated as a store barrier. It forces older store to be
113 // executed before newer stores are issued.
114 std::set<unsigned> StoreBarriers;
115
116 // An instruction that both 'MayLoad' and 'HasUnmodeledSideEffects' is
117 // conservatively treated as a load barrier. It forces older loads to execute
118 // before newer loads are issued.
119 std::set<unsigned> LoadBarriers;
120
121public:
122 LSUnit(unsigned LQ = 0, unsigned SQ = 0, bool AssumeNoAlias = false)
123 : LQ_Size(LQ), SQ_Size(SQ), NoAlias(AssumeNoAlias) {}
124
125#ifndef NDEBUG
126 void dump() const;
127#endif
128
129 bool isSQEmpty() const { return StoreQueue.empty(); }
130 bool isLQEmpty() const { return LoadQueue.empty(); }
131 bool isSQFull() const { return SQ_Size != 0 && StoreQueue.size() == SQ_Size; }
132 bool isLQFull() const { return LQ_Size != 0 && LoadQueue.size() == LQ_Size; }
133
Andrea Di Biagio2dee62b2018-03-22 14:14:49 +0000134 // Returns true if this instruction has been successfully enqueued.
135 bool reserve(unsigned Index, const InstrDesc &Desc);
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000136
137 // The rules are:
138 // 1. A store may not pass a previous store.
139 // 2. A load may not pass a previous store unless flag 'NoAlias' is set.
140 // 3. A load may pass a previous load.
141 // 4. A store may not pass a previous load (regardless of flag 'NoAlias').
142 // 5. A load has to wait until an older load barrier is fully executed.
143 // 6. A store has to wait until an older store barrier is fully executed.
144 bool isReady(unsigned Index) const;
145 void onInstructionExecuted(unsigned Index);
146};
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000147} // namespace mca
148
149#endif