brintos

brintos / llvm-project-archived public Read only

0
0
Text · 10.4 KiB · cef0f88 Raw
513 lines · plain
1{2 "cells": [3  {4   "cell_type": "markdown",5   "id": "c45bc113",6   "metadata": {},7   "source": [8    "# LLVM TableGen Kernel"9   ]10  },11  {12   "cell_type": "markdown",13   "id": "483313fc",14   "metadata": {},15   "source": [16    "This notebook is running `llvm-tblgen`."17   ]18  },19  {20   "cell_type": "code",21   "execution_count": 1,22   "id": "e238dd42",23   "metadata": {},24   "outputs": [25    {26     "name": "stdout",27     "output_type": "stream",28     "text": [29      "------------- Classes -----------------\n",30      "class Foo {\n",31      "}\n",32      "------------- Defs -----------------\n"33     ]34    }35   ],36   "source": [37    "%reset\n",38    "// This is some tablegen\n",39    "class Foo {}"40   ]41  },42  {43   "cell_type": "markdown",44   "id": "27f5aa83",45   "metadata": {},46   "source": [47    "Errors printed to stderr are shown."48   ]49  },50  {51   "cell_type": "code",52   "execution_count": 2,53   "id": "e8b10293",54   "metadata": {55    "scrolled": true56   },57   "outputs": [58    {59     "name": "stderr",60     "output_type": "stream",61     "text": [62      "<stdin>:1:1: error: Unexpected token at top level\n",63      "This is not tablegen.\n",64      "^\n"65     ]66    }67   ],68   "source": [69    "%reset\n",70    "This is not tablegen."71   ]72  },73  {74   "cell_type": "markdown",75   "id": "83907141",76   "metadata": {},77   "source": [78    "Add some classes to get some output."79   ]80  },81  {82   "cell_type": "code",83   "execution_count": 3,84   "id": "4ca8a9cb",85   "metadata": {},86   "outputs": [87    {88     "name": "stdout",89     "output_type": "stream",90     "text": [91      "------------- Classes -----------------\n",92      "class Stuff {\n",93      "}\n",94      "------------- Defs -----------------\n",95      "def thing {\t// Stuff\n",96      "}\n"97     ]98    }99   ],100   "source": [101    "%reset\n",102    "class Stuff {}\n",103    "def thing : Stuff {}"104   ]105  },106  {107   "cell_type": "markdown",108   "id": "3f29d1a0",109   "metadata": {},110   "source": [111    "By default cells are connected. Meaning that we cache the code and magic directives from the previously run cells.\n",112    "\n",113    "This means that the next cell still sees the `Stuff` class."114   ]115  },116  {117   "cell_type": "code",118   "execution_count": 4,119   "id": "3a204c70",120   "metadata": {121    "scrolled": true122   },123   "outputs": [124    {125     "name": "stdout",126     "output_type": "stream",127     "text": [128      "------------- Classes -----------------\n",129      "class Stuff {\n",130      "}\n",131      "------------- Defs -----------------\n",132      "def other_thing {\t// Stuff\n",133      "}\n",134      "def thing {\t// Stuff\n",135      "}\n"136     ]137    }138   ],139   "source": [140    "def other_thing : Stuff {}"141   ]142  },143  {144   "cell_type": "markdown",145   "id": "473b0a1c",146   "metadata": {},147   "source": [148    "You can use the magic `%reset` to clear this cache and start fresh."149   ]150  },151  {152   "cell_type": "code",153   "execution_count": 5,154   "id": "1f674a03",155   "metadata": {},156   "outputs": [157    {158     "name": "stderr",159     "output_type": "stream",160     "text": [161      "<stdin>:1:19: error: Couldn't find class 'Stuff'\n",162      "def other_thing : Stuff {}\n",163      "                  ^\n"164     ]165    }166   ],167   "source": [168    "%reset\n",169    "def other_thing : Stuff {}"170   ]171  },172  {173   "cell_type": "markdown",174   "id": "8f120cee",175   "metadata": {},176   "source": [177    "You can also configure the default reset behaviour using the `%config` magic."178   ]179  },180  {181   "cell_type": "code",182   "execution_count": 6,183   "id": "7c356853",184   "metadata": {},185   "outputs": [186    {187     "name": "stdout",188     "output_type": "stream",189     "text": [190      "------------- Classes -----------------\n",191      "class Thing {\n",192      "}\n",193      "------------- Defs -----------------\n"194     ]195    }196   ],197   "source": [198    "%config cellreset on\n",199    "class Thing {}"200   ]201  },202  {203   "cell_type": "code",204   "execution_count": 7,205   "id": "c5399401",206   "metadata": {},207   "outputs": [208    {209     "name": "stderr",210     "output_type": "stream",211     "text": [212      "<stdin>:2:13: error: Couldn't find class 'Thing'\n",213      "def AThing: Thing {}\n",214      "            ^\n"215     ]216    }217   ],218   "source": [219    "// The cache is reset here so this is an error.\n",220    "def AThing: Thing {}"221   ]222  },223  {224   "cell_type": "markdown",225   "id": "53220700",226   "metadata": {},227   "source": [228    "The default value is `off`, meaning cells are connected. If you want to override the default for one cell only, use the `%reset` or `%noreset` magic. These always override the default."229   ]230  },231  {232   "cell_type": "code",233   "execution_count": 8,234   "id": "50a865ea",235   "metadata": {},236   "outputs": [237    {238     "name": "stdout",239     "output_type": "stream",240     "text": [241      "------------- Classes -----------------\n",242      "class Thing {\n",243      "}\n",244      "------------- Defs -----------------\n"245     ]246    }247   ],248   "source": [249    "class Thing {}"250   ]251  },252  {253   "cell_type": "code",254   "execution_count": 9,255   "id": "3c079649",256   "metadata": {},257   "outputs": [258    {259     "name": "stdout",260     "output_type": "stream",261     "text": [262      "------------- Classes -----------------\n",263      "class Thing {\n",264      "}\n",265      "------------- Defs -----------------\n",266      "def AThing {\t// Thing\n",267      "}\n"268     ]269    }270   ],271   "source": [272    "%noreset\n",273    "// This works because of the noreset above.\n",274    "def AThing: Thing {}"275   ]276  },277  {278   "cell_type": "code",279   "execution_count": 10,280   "id": "75f3c51c",281   "metadata": {},282   "outputs": [283    {284     "name": "stderr",285     "output_type": "stream",286     "text": [287      "<stdin>:2:19: error: Couldn't find class 'Thing'\n",288      "def AnotherThing: Thing {}\n",289      "                  ^\n"290     ]291    }292   ],293   "source": [294    "// This does not because we're not changing the default.\n",295    "def AnotherThing: Thing {}"296   ]297  },298  {299   "cell_type": "code",300   "execution_count": 11,301   "id": "9abe502e",302   "metadata": {},303   "outputs": [304    {305     "name": "stdout",306     "output_type": "stream",307     "text": [308      "------------- Classes -----------------\n",309      "------------- Defs -----------------\n"310     ]311    }312   ],313   "source": [314    "%config cellreset off\n",315    "%reset\n",316    "// Here we have an empty cache and default reset behaviour."317   ]318  },319  {320   "cell_type": "markdown",321   "id": "0de42c3c",322   "metadata": {},323   "source": [324    "It is not valid to have `%reset` and `%noreset` in the same cell."325   ]326  },327  {328   "cell_type": "code",329   "execution_count": 12,330   "id": "a763daa4",331   "metadata": {},332   "outputs": [333    {334     "name": "stderr",335     "output_type": "stream",336     "text": [337      "%reset and %noreset in the same cell is not allowed. Use only one, or neither."338     ]339    }340   ],341   "source": [342    "%reset\n",343    "%noreset"344   ]345  },346  {347   "cell_type": "markdown",348   "id": "f6d4613b",349   "metadata": {},350   "source": [351    "Consider setting `cellreset` to the majority usecase for your notebook. For example a tutorial building a large example across many cells will likely want it `off`. One with many standalone examples, `on`.\n",352    "\n",353    "There is a \"magic\" directive `%args` that you can use to send command line arguments to `llvm-tblgen`.\n",354    "\n",355    "For example, here we have some code that shows a warning."356   ]357  },358  {359   "cell_type": "code",360   "execution_count": 13,361   "id": "2586893b",362   "metadata": {},363   "outputs": [364    {365     "name": "stderr",366     "output_type": "stream",367     "text": [368      "<stdin>:1:25: warning: unused template argument: Thing:B\n",369      "class Thing <int A, int B> {\n",370      "                        ^\n"371     ]372    }373   ],374   "source": [375    "%reset\n",376    "class Thing <int A, int B> {\n",377    "    int num = A;\n",378    "}"379   ]380  },381  {382   "cell_type": "markdown",383   "id": "41be44e7",384   "metadata": {},385   "source": [386    "We can pass an argument to ignore that warning."387   ]388  },389  {390   "cell_type": "code",391   "execution_count": 14,392   "id": "ae078bc4",393   "metadata": {},394   "outputs": [395    {396     "name": "stdout",397     "output_type": "stream",398     "text": [399      "------------- Classes -----------------\n",400      "class Thing<int Thing:A = ?, int Thing:B = ?> {\n",401      "  int num = Thing:A;\n",402      "}\n",403      "------------- Defs -----------------\n"404     ]405    }406   ],407   "source": [408    "%args --no-warn-on-unused-template-args"409   ]410  },411  {412   "cell_type": "markdown",413   "id": "316b9543",414   "metadata": {},415   "source": [416    "If you have a run of cells without a `%reset`, the most recent `%args` is used."417   ]418  },419  {420   "cell_type": "code",421   "execution_count": 15,422   "id": "9634170c",423   "metadata": {},424   "outputs": [425    {426     "name": "stdout",427     "output_type": "stream",428     "text": [429      "------------- Classes -----------------\n",430      "class Thing<int Thing:A = ?, int Thing:B = ?> {\n",431      "  int num = Thing:A;\n",432      "}\n",433      "------------- Defs -----------------\n"434     ]435    }436   ],437   "source": [438    "// This passes --no-warn-on-unused-template-args"439   ]440  },441  {442   "cell_type": "code",443   "execution_count": 16,444   "id": "fa15b542",445   "metadata": {},446   "outputs": [447    {448     "name": "stderr",449     "output_type": "stream",450     "text": [451      "<stdin>:1:25: warning: unused template argument: Thing:B\n",452      "class Thing <int A, int B> {\n",453      "                        ^\n"454     ]455    }456   ],457   "source": [458    "%args\n",459    "// Now we're not passing the argument so the warning comes back."460   ]461  },462  {463   "cell_type": "markdown",464   "id": "e112f1d4",465   "metadata": {},466   "source": [467    "If there are many `%args` in a cell, the last one is used."468   ]469  },470  {471   "cell_type": "code",472   "execution_count": 17,473   "id": "2ac46c7a",474   "metadata": {},475   "outputs": [476    {477     "name": "stderr",478     "output_type": "stream",479     "text": [480      "<stdin>:1:18: warning: unused template argument: Thing:A\n",481      "class Thing <int A, int B> {}\n",482      "                 ^\n",483      "<stdin>:1:25: warning: unused template argument: Thing:B\n",484      "class Thing <int A, int B> {}\n",485      "                        ^\n"486     ]487    }488   ],489   "source": [490    "%reset\n",491    "%args --no-warn-on-unused-template-args\n",492    "%args\n",493    "class Thing <int A, int B> {}"494   ]495  }496 ],497 "metadata": {498  "kernelspec": {499   "display_name": "LLVM TableGen",500   "language": "tablegen",501   "name": "tablegen"502  },503  "language_info": {504   "file_extension": ".td",505   "mimetype": "text/x-tablegen",506   "name": "tablegen",507   "pygments_lexer": "text"508  }509 },510 "nbformat": 4,511 "nbformat_minor": 5512}513