Merged
refactor(codegen): Refactor print of op comment #1646
refactor(codegen): Refactor print of op comment #1646
Merged
HujunAdd on Mar 16
Total: 9 files changed+27-79
@@ -140,10 +140,6 @@ void CodeGenCloudNPU::GenFuncBody(Function &subFunc, Function &topFunc, std::ost
140 << "Generate code of op failed, op is " << op.Dump();140 << "Generate code of op failed, op is " << op.Dump();
141 141 
142 allocSourceRegion.append(allocSourceCode);142 allocSourceRegion.append(allocSourceCode);
143- 
144- for (auto &c : op.GetCommentList()) {
145- tileOpSourceRegion.append("/*").append(c).append("*/\n");
146- }
147 tileOpSourceRegion.append(tileOpSourceCode);143 tileOpSourceRegion.append(tileOpSourceCode);
148 144 
149 if (!allocSourceCode.empty()) {145 if (!allocSourceCode.empty()) {
@@ -380,7 +376,7 @@ std::optional<std::string> CodeGenCloudNPU::GenExtraAlloc(
380 auto memType = tensor->GetMemoryTypeOriginal();376 auto memType = tensor->GetMemoryTypeOriginal();
381 if (OPERAND_TYPE_TO_MEMORY_TYPE.find(memType) == OPERAND_TYPE_TO_MEMORY_TYPE.end()) {377 if (OPERAND_TYPE_TO_MEMORY_TYPE.find(memType) == OPERAND_TYPE_TO_MEMORY_TYPE.end()) {
382 CODEGEN_LOGE_E(OperErr::OPERAND_TYPE_UNSUPPORTED,378 CODEGEN_LOGE_E(OperErr::OPERAND_TYPE_UNSUPPORTED,
383- " memory type(%zu) of tensor from PASS is invalid, tensor is: %s", static_cast<size_t>(memType),379+ " memory type(%u) of tensor from PASS is invalid, tensor is: %s", ToUnderlying(memType),
384 tensor->Dump().c_str());380 tensor->Dump().c_str());
385 return std::nullopt;381 return std::nullopt;
386 }382 }
@@ -422,7 +418,7 @@ std::string CodeGenCloudNPU::GenAlloc(
422 return "";418 return "";
423 }419 }
424 420 
425- CODEGEN_LOGI("%s: bind key to name: %s->%s", __FUNCTION__, sm->FormatAllocKey(key).c_str(), allocVarName.c_str());421+ CODEGEN_LOGI("bind key to name: %s->%s", sm->FormatAllocKey(key).c_str(), allocVarName.c_str());
426 422 
427 std::string dataTypeStr = DataType2CCEStr(dataType);423 std::string dataTypeStr = DataType2CCEStr(dataType);
428 424 
@@ -733,6 +733,15 @@ TileTensor CodeGenOpCloudNPU::QueryTileTensorByIdx(int paramIdx) const {
733 return emptyTileTensor;733 return emptyTileTensor;
734}734}
735 735 
736+std::string CodeGenOpCloudNPU::InsertOpComment(const std::string &tileOpSourceCode) const {
737+ std::ostringstream os;
738+ for (auto &c : originalOp.GetCommentList()) {
739+ os << "/*" << c << "*/\n";
740+ }
741+ os << tileOpSourceCode;
742+ return os.str();
743+}
744+ 
736std::string CodeGenOpCloudNPU::QueryTileTensorNameByIdx(int paramIdx) const {745std::string CodeGenOpCloudNPU::QueryTileTensorNameByIdx(int paramIdx) const {
737 const TileTensor &tileTensor = QueryTileTensorByIdx(paramIdx);746 const TileTensor &tileTensor = QueryTileTensorByIdx(paramIdx);
738 return tileTensor.tensorName;747 return tileTensor.tensorName;
@@ -744,17 +753,19 @@ std::string CodeGenOpCloudNPU::QueryTileTensorTypeByIdx(int paramIdx) const {
744}753}
745 754 
746std::string CodeGenOpCloudNPU::GenOpCode() const {755std::string CodeGenOpCloudNPU::GenOpCode() const {
747- std::string ret;756+ std::string tileOpSourceCode;
748 auto iter = opsGenMap_.find(opCode);757 auto iter = opsGenMap_.find(opCode);
749 if (iter != opsGenMap_.end()) {758 if (iter != opsGenMap_.end()) {
750- ret = iter->second();759+ tileOpSourceCode = iter->second();
751 } else {760 } else {
752 // To aid in testing, do not use ASSERT.761 // To aid in testing, do not use ASSERT.
753 return std::string{"CAN NOT HANDLE OP: " + opCodeStr};762 return std::string{"CAN NOT HANDLE OP: " + opCodeStr};
754 }763 }
755 764 
765+ std::string ret = InsertOpComment(tileOpSourceCode);
766+ 
756 if (forBlkMgr_ == nullptr || !forBlkMgr_->IsInLoop()) {767 if (forBlkMgr_ == nullptr || !forBlkMgr_->IsInLoop()) {
757- CODEGEN_LOGI_FULL(": op codegen result: \n, %s", ret.c_str());768+ CODEGEN_LOGI_FULL("op codegen result: \n, %s", ret.c_str());
758 return ret;769 return ret;
759 }770 }
760 771 
@@ -768,7 +779,7 @@ std::string CodeGenOpCloudNPU::GenOpCode() const {
768 779 
769 ret = forBlkMgr_->Print();780 ret = forBlkMgr_->Print();
770 forBlkMgr_->OutLoop();781 forBlkMgr_->OutLoop();
771- CODEGEN_LOGI_FULL(": op codegen result: \n, %s", ret.c_str());782+ CODEGEN_LOGI_FULL("op codegen result: \n, %s", ret.c_str());
772 return ret;783 return ret;
773}784}
774 785 
@@ -170,6 +170,7 @@ public:
170 170 
171private:171private:
172 TileTensor QueryTileTensorByIdx(int paramIdx) const;172 TileTensor QueryTileTensorByIdx(int paramIdx) const;
173+ std::string InsertOpComment(const std::string &tileOpSourceCode) const;
173 174 
174 std::string GenTemplateParamsForPutAndGet() const;175 std::string GenTemplateParamsForPutAndGet() const;
175 std::string GenTemplateParamsForPutUb2Gm() const;176 std::string GenTemplateParamsForPutUb2Gm() const;
@@ -450,8 +450,8 @@ void CodeGenOp::GetGmParamIdx(const Operation &oper) {
450 ASSERT(OperErr::OPERAND_COUNT_NOT_MATCHED, outParamLocSize <= oper.oOperand.size())450 ASSERT(OperErr::OPERAND_COUNT_NOT_MATCHED, outParamLocSize <= oper.oOperand.size())
451 << "size of Op.outParamLocation_ is larger than output operands, Op is " << oper.Dump();451 << "size of Op.outParamLocation_ is larger than output operands, Op is " << oper.Dump();
452 452 
453- CODEGEN_LOGI("%s: inParamLocation = %s", __FUNCTION__, IntVecToStr(oper.inParamLocation_).c_str());453+ CODEGEN_LOGI("inParamLocation = %s", IntVecToStr(oper.inParamLocation_).c_str());
454- CODEGEN_LOGI("%s: outParamLocation = %s", __FUNCTION__, IntVecToStr(oper.outParamLocation_).c_str());454+ CODEGEN_LOGI("outParamLocation = %s", IntVecToStr(oper.outParamLocation_).c_str());
455 455 
456 std::copy(oper.outParamLocation_.begin(), oper.outParamLocation_.end(), paramLocation);456 std::copy(oper.outParamLocation_.begin(), oper.outParamLocation_.end(), paramLocation);
457 std::copy(oper.inParamLocation_.begin(), oper.inParamLocation_.end(), paramLocation + oper.oOperand.size());457 std::copy(oper.inParamLocation_.begin(), oper.inParamLocation_.end(), paramLocation + oper.oOperand.size());
@@ -498,7 +498,7 @@ void CodeGenOp::GetGmParamIdx(const Operation &oper) {
498 paramLocation[1] = oper.GetIOpAttrOffset(0);498 paramLocation[1] = oper.GetIOpAttrOffset(0);
499 CODEGEN_LOGI("Gm Param Index of Copy In Op %s is %d", tileOpName.c_str(), paramLocation[1]);499 CODEGEN_LOGI("Gm Param Index of Copy In Op %s is %d", tileOpName.c_str(), paramLocation[1]);
500 GmTensorParamIdxInCallFunc = oper.GetIntAttribute("GmTensorParamIdxInCallFunc");500 GmTensorParamIdxInCallFunc = oper.GetIntAttribute("GmTensorParamIdxInCallFunc");
501- CODEGEN_LOGI("%s GmTensorParamIdxInCallFunc: %d", __FUNCTION__, GmTensorParamIdxInCallFunc);501+ CODEGEN_LOGI("GmTensorParamIdxInCallFunc: %d", GmTensorParamIdxInCallFunc);
502 return;502 return;
503 }503 }
504 504 
@@ -509,7 +509,7 @@ void CodeGenOp::GetGmParamIdx(const Operation &oper) {
509 paramLocation[0] = oper.GetOOpAttrOffset(0);509 paramLocation[0] = oper.GetOOpAttrOffset(0);
510 CODEGEN_LOGI("Gm Param Index of Copy Out Op %s is %d", tileOpName.c_str(), paramLocation[0]);510 CODEGEN_LOGI("Gm Param Index of Copy Out Op %s is %d", tileOpName.c_str(), paramLocation[0]);
511 GmTensorParamIdxInCallFunc = oper.GetIntAttribute("GmTensorParamIdxInCallFunc");511 GmTensorParamIdxInCallFunc = oper.GetIntAttribute("GmTensorParamIdxInCallFunc");
512- CODEGEN_LOGI("%s GmTensorParamIdxInCallFunc: %d", __FUNCTION__, GmTensorParamIdxInCallFunc);512+ CODEGEN_LOGI("GmTensorParamIdxInCallFunc: %d", GmTensorParamIdxInCallFunc);
513 return;513 return;
514 }514 }
515}515}
@@ -75,7 +75,7 @@ std::string SymbolManager::FormatAllocKey(const AllocKey &key) {
75}75}
76 76 
77std::string SymbolManager::QueryVariableName(const AllocKey &key) {77std::string SymbolManager::QueryVariableName(const AllocKey &key) {
78- CODEGEN_LOGI("%s: query varname by identifier: %s", __FUNCTION__, FormatAllocKey(key).c_str());78+ CODEGEN_LOGI("query varname by identifier: %s", FormatAllocKey(key).c_str());
79 auto iter = key2VariableName_.find(key);79 auto iter = key2VariableName_.find(key);
80 ASSERT(GenCodeErr::SYMBOL_NOT_FOUND, iter != key2VariableName_.end())80 ASSERT(GenCodeErr::SYMBOL_NOT_FOUND, iter != key2VariableName_.end())
81 << "QueryVariableName Failed: UNDEFINED_VAR !!! AllocKey: " << FormatAllocKey(key);81 << "QueryVariableName Failed: UNDEFINED_VAR !!! AllocKey: " << FormatAllocKey(key);
@@ -83,7 +83,7 @@ std::string SymbolManager::QueryVariableName(const AllocKey &key) {
83}83}
84 84 
85std::string SymbolManager::QueryVariableNameTileTensor(const AllocKey &key) {85std::string SymbolManager::QueryVariableNameTileTensor(const AllocKey &key) {
86- CODEGEN_LOGI("%s: query varname TileTensor mode by identifier: %s", __FUNCTION__, FormatAllocKey(key).c_str());86+ CODEGEN_LOGI("query varname TileTensor mode by identifier: %s", FormatAllocKey(key).c_str());
87 87 
88 auto iter = key2VariableNameTileTensor_.find(key);88 auto iter = key2VariableNameTileTensor_.find(key);
89 if (iter != key2VariableNameTileTensor_.end()) {89 if (iter != key2VariableNameTileTensor_.end()) {
@@ -211,29 +211,14 @@ TEST_F(TestCodegenDynIndexOutCast, DynIndexOutUnaligned) {
211#else211#else
212 auto function = Program::GetInstance().GetFunctionByRawName(FUNCTION_PREFIX + funcName + SUB_FUNC_SUFFIX);212 auto function = Program::GetInstance().GetFunctionByRawName(FUNCTION_PREFIX + funcName + SUB_FUNC_SUFFIX);
213#endif213#endif
214- for (auto &subFunc : function->rootFunc_->programs_) {214+ 
215- for (auto &op : subFunc.second->Operations()) {
216- if (OpcodeManager::Inst().IsCopyIn(op.GetOpcode()) || OpcodeManager::Inst().IsCopyOut(op.GetOpcode())) {
217- if (IsCopyIn(op.GetOpcode()))
218- op.SetIOpAttrOffset(0, 0);
219- else
220- op.SetOOpAttrOffset(0, 0);
221- op.SetAttribute("GmTensorParamIdxInCallFunc", 0);
222- }
223- }
224- DynParamInfo fakeParam = {2, 0, 1, DynParamInfoType::VALID_SHAPE, 0, SymbolicScalar(), false, ""};
225- subFunc.second->InsertDynParam("sym_32_dim_0", fakeParam);
226- subFunc.second->InsertDynParam("sym_32_dim_1", fakeParam);
227- subFunc.second->InsertDynParam("sym_38_dim_0", fakeParam);
228- subFunc.second->InsertDynParam("sym_38_dim_1", fakeParam);
229- }
230 npu::tile_fwk::CodeGenCtx ctx;215 npu::tile_fwk::CodeGenCtx ctx;
231 npu::tile_fwk::CodeGenCloudNPU codeGen(ctx);216 npu::tile_fwk::CodeGenCloudNPU codeGen(ctx);
232 codeGen.GenCode(*function, {});217 codeGen.GenCode(*function, {});
233 218 
234 std::string res = GetResultFromCpp(*function);219 std::string res = GetResultFromCpp(*function);
235 std::string expect =220 std::string expect =
236- R"!!!(TileOp::DynTIndexoutcast<int32_t, int32_t, 1, 32, 32, 32, 0, 1>((__gm__ int32_t*)GET_PARAM_ADDR(param, 0, 0), (__ubuf__ int32_t*)UB_S0_E4096, (__ubuf__ int32_t*)UB_S4096_E8192, 1, 1, sym_6_dim_1, sym_9_dim_0, sym_9_dim_1, 1, 1, GET_PARAM_RAWSHAPE_2(param, 0, 0), 0, 0, (RUNTIME_COA_GET_PARAM_OFFSET(2, 28, 0)), (RUNTIME_COA_GET_PARAM_OFFSET(2, 28, 1)));221+ R"!!!(TileOp::DynTIndexoutcast<int32_t, int32_t, 1, 32, 32, 32, 0, 1>((__gm__ int32_t*)GET_PARAM_ADDR(param, 2, 28), (__ubuf__ int32_t*)UB_S0_E4096, (__ubuf__ int32_t*)UB_S4096_E8192, 1, 1, sym_6_dim_1, sym_9_dim_0, sym_9_dim_1, 1, 1, GET_PARAM_RAWSHAPE_2(param, 2, 28), 0, 0, (RUNTIME_COA_GET_PARAM_OFFSET(2, 28, 0)), (RUNTIME_COA_GET_PARAM_OFFSET(2, 28, 1)));
237)!!!";222)!!!";
238 CheckStringExist(expect, res);223 CheckStringExist(expect, res);
239}224}
@@ -174,22 +174,7 @@ TEST_F(TestCodegenDynBinary, TestGatherEle) {
174 Program::GetInstance().GetFunctionByRawName(FUNCTION_PREFIX + funcName + SUB_FUNC_SUFFIX + HIDDEN_FUNC_SUFFIX);174 Program::GetInstance().GetFunctionByRawName(FUNCTION_PREFIX + funcName + SUB_FUNC_SUFFIX + HIDDEN_FUNC_SUFFIX);
175 function->SetFunctionType(FunctionType::DYNAMIC_LOOP_PATH);175 function->SetFunctionType(FunctionType::DYNAMIC_LOOP_PATH);
176 function->SetUnderDynamicFunction(true);176 function->SetUnderDynamicFunction(true);
177- for (auto &subFunc : function->rootFunc_->programs_) {177+ 
178- for (auto &op : subFunc.second->Operations()) {
179- if (OpcodeManager::Inst().IsCopyIn(op.GetOpcode()) || OpcodeManager::Inst().IsCopyOut(op.GetOpcode())) {
180- if (IsCopyIn(op.GetOpcode()))
181- op.SetIOpAttrOffset(0, 0);
182- else
183- op.SetOOpAttrOffset(0, 0);
184- op.SetAttribute("GmTensorParamIdxInCallFunc", 0);
185- }
186- }
187- DynParamInfo fakeParam = {2, 0, 0, DynParamInfoType::VALID_SHAPE, 0, SymbolicScalar(), false, ""};
188- subFunc.second->InsertDynParam("sym_2_dim_0", fakeParam);
189- subFunc.second->InsertDynParam("sym_2_dim_1", fakeParam);
190- subFunc.second->InsertDynParam("sym_4_dim_0", fakeParam);
191- subFunc.second->InsertDynParam("sym_4_dim_1", fakeParam);
192- }
193 npu::tile_fwk::CodeGenCtx ctx;178 npu::tile_fwk::CodeGenCtx ctx;
194 npu::tile_fwk::CodeGenCloudNPU codeGen(ctx);179 npu::tile_fwk::CodeGenCloudNPU codeGen(ctx);
195 codeGen.GenCode(*function, {});180 codeGen.GenCode(*function, {});
@@ -73,21 +73,6 @@ TEST_F(TestCodegenDynRowReduceLine, TestOperationRowSumLine) {
73 Program::GetInstance().GetFunctionByRawName(FUNCTION_PREFIX + funcName + SUB_FUNC_SUFFIX + HIDDEN_FUNC_SUFFIX);73 Program::GetInstance().GetFunctionByRawName(FUNCTION_PREFIX + funcName + SUB_FUNC_SUFFIX + HIDDEN_FUNC_SUFFIX);
74 function->SetFunctionType(FunctionType::DYNAMIC_LOOP_PATH);74 function->SetFunctionType(FunctionType::DYNAMIC_LOOP_PATH);
75 function->SetUnderDynamicFunction(true);75 function->SetUnderDynamicFunction(true);
76- for (auto &subFunc : function->rootFunc_->programs_) {
77- for (auto &op : subFunc.second->Operations()) {
78- if (OpcodeManager::Inst().IsCopyIn(op.GetOpcode()) || OpcodeManager::Inst().IsCopyOut(op.GetOpcode())) {
79- if (IsCopyIn(op.GetOpcode()))
80- op.SetIOpAttrOffset(0, 0);
81- else
82- op.SetOOpAttrOffset(0, 0);
83- op.SetAttribute("GmTensorParamIdxInCallFunc", 0);
84- }
85- }
86- DynParamInfo fakeParam = {3, 0, 0, DynParamInfoType::VALID_SHAPE, 0, SymbolicScalar(), false, ""};
87- subFunc.second->InsertDynParam("sym_23_dim_0", fakeParam);
88- subFunc.second->InsertDynParam("sym_23_dim_1", fakeParam);
89- subFunc.second->InsertDynParam("sym_23_dim_2", fakeParam);
90- }
91 76 
92 npu::tile_fwk::CodeGenCtx ctx;77 npu::tile_fwk::CodeGenCtx ctx;
93 npu::tile_fwk::CodeGenCloudNPU codeGen(ctx);78 npu::tile_fwk::CodeGenCloudNPU codeGen(ctx);
Mframework/tests/ut/codegen/src/test_static/test_vector/test_codegen_row_sum_line.cpp+0-15File content is under review, please try again later