xref: /aosp_15_r20/external/pytorch/torch/csrc/profiler/unwind/action.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #pragma once
2 #include <stdint.h>
3 #include <ostream>
4 
5 namespace torch::unwind {
6 
7 enum {
8   A_UNDEFINED = 0x0,
9   A_REG_PLUS_DATA = 0x1, // exp = REG[reg] + data0
10   A_LOAD_CFA_OFFSET = 0x2, // exp = *(cfa + data0)
11   A_REG_PLUS_DATA_DEREF = 0x3 // exp = *(REG[reg] + data0)
12 };
13 
14 // register numbers in dwarf info
15 enum {
16   D_UNDEFINED = -1,
17   D_RBP = 6,
18   D_RSP = 7,
19   D_RIP = 16,
20   D_REG_SIZE = 17,
21 };
22 
23 struct Action {
24   uint8_t kind = A_UNDEFINED;
25   int32_t reg = -1;
26   int64_t data = 0;
undefinedAction27   static Action undefined() {
28     return Action{A_UNDEFINED};
29   }
regPlusDataAction30   static Action regPlusData(int32_t reg, int64_t offset) {
31     return Action{A_REG_PLUS_DATA, reg, offset};
32   }
regPlusDataDerefAction33   static Action regPlusDataDeref(int32_t reg, int64_t offset) {
34     return Action{A_REG_PLUS_DATA_DEREF, reg, offset};
35   }
loadCfaOffsetAction36   static Action loadCfaOffset(int64_t offset) {
37     return Action{A_LOAD_CFA_OFFSET, D_UNDEFINED, offset};
38   }
39 
40   friend std::ostream& operator<<(std::ostream& out, const Action& self) {
41     switch (self.kind) {
42       case A_UNDEFINED:
43         out << "u";
44         break;
45       case A_REG_PLUS_DATA:
46         out << "r" << (int)self.reg << " + " << self.data;
47         break;
48       case A_REG_PLUS_DATA_DEREF:
49         out << "*(r" << (int)self.reg << " + " << self.data << ")";
50         break;
51       case A_LOAD_CFA_OFFSET:
52         out << "*(cfa + " << self.data << ")";
53         break;
54     }
55     return out;
56   }
57 };
58 
59 } // namespace torch::unwind
60