[clang] [clang] Fix TemplateInstantiator crash transforming loop hint argument (PR #172289)

Bruno De Fraine via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 7 14:08:11 PST 2026


================
@@ -2197,19 +2197,23 @@ TemplateInstantiator::TransformLoopHintAttr(const LoopHintAttr *LH) {
 
   // Generate error if there is a problem with the value.
   if (getSema().CheckLoopHintExpr(TransformedExpr, LH->getLocation(),
-                                  LH->getSemanticSpelling() ==
+                                  /*AllowZero=*/LH->getSemanticSpelling() ==
                                       LoopHintAttr::Pragma_unroll))
     return LH;
 
   LoopHintAttr::OptionType Option = LH->getOption();
   LoopHintAttr::LoopHintState State = LH->getState();
 
-  llvm::APSInt ValueAPS =
-      TransformedExpr->EvaluateKnownConstInt(getSema().getASTContext());
-  // The values of 0 and 1 block any unrolling of the loop.
-  if (ValueAPS.isZero() || ValueAPS.isOne()) {
-    Option = LoopHintAttr::Unroll;
-    State = LoopHintAttr::Disable;
+  if (Option == LoopHintAttr::UnrollCount &&
----------------
brunodf-snps wrote:

I've added an extra test case to clang/test/CodeGenCXX/pragma-unroll.cpp for the bug that surfaced here (see commit 3868ec2e2). It verifies that a loop with vectorize_width(1) does not get "unroll disable" metadata.

While doing this, I discovered that this test (and sibling test pragma-gcc-unroll.cpp) was not adequately verifying the generated metadata (although that was clearly the intention). That made it very hard to add my new test case in a meaningful way. So I first had to do commit 7fd8afdb to correct the FileCheck patterns for the metadata (the commit message explains in more detail what was wrong). I think this commit 7fd8afdb does not belong in this PR and could be pre-committed. What do you think?

https://github.com/llvm/llvm-project/pull/172289


More information about the cfe-commits mailing list