跳轉到內容

ROSE 編譯器框架/OpenK

來自華夏公益教科書

一個正在進行的專案,旨在探索知識驅動的高效能計算分析和最佳化。我們使用 OWL 使用的標準和工具鏈來正式建模高效能計算領域中的概念和關係,包括程式、硬體、分析和最佳化等。

該框架包含多個部分

  • 一個是生成資料庫的 C 程式解析器。
    • 原始碼位於 OpenK/tools/rosePrgKnowledgeBuilder 下,它從輸入程式中提取知識並將其儲存為烏龜格式的本體。
    • 輸出的知識庫可以使用 SWI-Prolog semweb 庫解析。
  • 第二部分是用 Prolog 編寫的規範迴圈分析部分。
  • 第三部分是使用 Prolog C++ 介面查詢硬體本體的演示

OpenK 已整合到 ROSE 中,位於

以下文件有點過時,需要更新。

目錄佈局

[編輯 | 編輯原始碼]

列表

  • ontology/ - owl 檔案。
  • projects/ - 包含獨立的專案或程式:規範迴圈檢測、指標分析等。
    • canonicalloop/ - 使用 Prolog 查詢查詢規範迴圈
    • cfg_test/ - 使用 Prolog 生成控制流圖
    • rose_canonicalloop/ - 經典的基於 AST 的規範迴圈檢測,用於比較
    • staticCFG_tests/- 經典的基於 AST 的控制流圖生成,用於比較
  • test/ - 測試基準和指令碼
  • tools/ - 知識生成器:使用 ROSE 前端解析 C 程式碼並構建知識庫。

軟體依賴

[編輯 | 編輯原始碼]

您需要安裝

  • ROSE
  • SWI-Prolog

使用者指南

[編輯 | 編輯原始碼]

要構建翻譯器

  1. 透過 $ . openk/set.rose 設定 ROSE 環境。注意,set.rose 檔案應根據您的機器進行修改。
    1. 或者只需手動匯出 ROSE_INS=/home/liao6/workspace/masterDevClean/install
    2. export BOOST_INS=/nfs/casc/overture/ROSE/opt/rhel7/x86_64/boost/1_54_0/gcc/4.8.3
  2. cd 到 openk/tools/rosePrgKnowledgeBuilder 資料夾。
  3. 您可以執行make 來生成 rosePrgKnowledgeBuilder.exe,並執行 make check 來檢查它是否與同一目錄下的測試檔案相符。

rosePrgKnowledgeBuilder.exe

[編輯 | 編輯原始碼]

./rosePrgKnowledgeBuilder.exe -emit-ttl ut.cc.ttl -c ./ut.cc


openk.git/tools/rosePrgKnowledgeBuilder]./rosePrgKnowledgeBuilder.exe --help

---------------------Tool-Specific Help-----------------------------------
This is a source translator to build knowledge base from your C/C++ code.
Usage: ./rosePrgKnowledgeBuilder.exe[options][-o output]-c input.c

Options:
        --help              This help message.
        -emit-pl output.pl       ontologies in prolog file.
        -emit-owl output.owl     ontologies in OWL format.
        -emit-ttl output.ttl     ontologies in Turtle.
        -alive true/false        embedding Prolog engine.

宣告式規範迴圈分析

[編輯 | 編輯原始碼]

宣告式分析工具位於 openk/projects/canonicalloop

  1. openk/csem 是實現 C 程式基本分析的 Prolog 模組,包括搜尋不同型別的程式結構。它們可以被視為庫模組。歡迎在 csem 下新增更多模組以擴充套件庫。
  2. 每個基於 prolog 的 c 程式分析都應該有自己的目錄,其中包含一個 run.pl 作為驅動程式。它們使用 csem 中的模組。

例如,規範迴圈分析位於 openk/projects/canonicalloop 下。

執行swipl -s run.pl example_knowledge_base.ttl output_report.txt

規範迴圈的參考 rose 原生 C++ 實現位於 openk/projects/rose_canonicalloop 下。

執行 make 來生成 roseNativeCl.exe

要針對測試臺測試它

  1. cd openk/test/npb2.3-test/
  2. 有一個用於呼叫測試的指令碼。該指令碼使用工具 rosePrgKnowledgeBuilder.exe 和 roseNativeCl.exe 以及 prolog 程式。您無需執行任何操作,指令碼使用這些工具的相對路徑。
  3. $chmod +x analysis.py
  4. ./analysis.py rose-cl 執行原生 Rose 規範迴圈執行。輸出日誌將生成在當前資料夾下。
  5. ./analysis.py build-kb 執行解析器以生成資料庫,然後 ./analysis openk-cl 在所有測試檔案上執行基於 prolog 的規範迴圈。
  6. 或者簡單來說,您只需執行./analysis.py all 即可執行所有測試。

宣告式控制流圖

[編輯 | 編輯原始碼]

cd projects/cfg_test

要從 ttl 檔案生成 CFG 報告

  • swipl --nosignal --quiet run.pl sp_single.c.ttl sp_single.c.ttl_openk_cfg_report.txt

硬體本體演示

[編輯 | 編輯原始碼]

openk.git/projects/owl_hw_demo

  • hpchardware-rdf.owl 高效能計算硬體本體
  • load2.pl Prolog 謂詞,用於透過 Prolog 語義 Web 庫讀取本體
  • main.cpp C++ 程式,透過 SWI-Prolog C++ 介面與本體互動

我們提供一個 Python 指令碼用於執行各種測試:cd /test/npb2.3-test

./run_test.py <option>

選項可以是以下之一

clean: clean the result files.
build-kb: build knowledge base from the input codes
openk-cl: run the openk canonical loop analysis. Must after the build-kb step
rose-cl: run the rose native canonical loop analysis
openk-cfg: openk cfg

關鍵原始檔

[編輯 | 編輯原始碼]

列表

  • openk.git/tools/rosePrgKnowledgeBuilder/
    • rosePrgKnowledgeBuilder.cpp
    • OntoGeneratorInterface.hpp

命令列

  • swipl --nosignal --quiet /export/liao6/openk.git/projects/canonicalloop/run.pl bt.c.ttl bt.c.pl_openk_cl_report.txt

./projects/canonicalloop/run.pl

:- use_module(library(semweb/rdf_db)).
:- use_module(library(semweb/turtle)).
:- use_module('../csem/canonical_loop.pl').
:- use_module('../csem/url_process.pl').

./projects/csem/canonical_loop.pl

/* CanonicalLoop */
:- module(canonical_loop, [canonicalLoop/1]).
:- use_module(c_construct).

%%	The top level specification
canonicalLoop(LoopURL) :-
	forLoop(LoopURL),
	hasForInit(LoopURL, Init),
	isCanonicalInit(Init, LoopVar),

	hasForTest(LoopURL, Test),
	isCanonicalTest(Test, LoopVar),

	hasForIncr(LoopURL, Incr),
	isCanonicalIncr(Incr, LoopVar).

isCanonicalInit(InitURL, LoopVar) :-
	%% forLoop(LoopURL), hasForInit(LoopURL, InitURL),
	roseCanonicalInit(InitURL, LoopVar, _),
	hasType(LoopVar, Type), 
	(intType(Type); Type == pointer_type).

%% normal style
roseCanonicalInit(InitURL, LoopVar, LB) :-
	hasChild(InitURL, AssignOpURL),
	c_is_a(AssignOpURL, 'AssignOp'), !,
	leftOperand(AssignOpURL, VarRefURL),
	get_varDecl(VarRefURL, LoopVar),
	rightOperand(AssignOpURL, LB).

%% C99 style
%% In this case, rose AST nodes have the same location
roseCanonicalInit(InitURL, LoopVar, LB) :-
	%% hasChild(InitURL, VarURL),
	c_is_a(InitURL, 'VariableDecl'),
	c_is_a(InitURL, 'Variable'), !,
	\+ (hasChild(InitURL, VarDecl), c_is_a(VarDecl, 'VariableDecl')),
	roseHasInitValue(InitURL, LB),
	LoopVar = InitURL.
...
華夏公益教科書