Skip to content

Conversation

@pantha704
Copy link

@pantha704 pantha704 commented Jan 24, 2026

Summary

Fixes #2553 - WrapGeneric was forcing all outputs to secret types, causing downstream crashes for functions that return values not depending on secrets.

Changes

Core Change: Operation Partitioning

Instead of wrapping ALL function body operations in secret.generic, this PR:

  1. Classifies operations using SecretnessAnalysis - tracks which values depend on secrets
  2. Partitions operations into secret-dependent vs plaintext
  3. Only wraps secret-dependent ops in secret.generic
  4. Keeps plaintext operations outside the generic block

Key Algorithm

Phase 1: Track secret values (starting from secret arguments)
Phase 2: Mark ops as secret if any operand is secret
Phase 3: Determine which outputs are secret vs plaintext
Phase 4: Build IR with plaintext ops outside, secret ops inside generic

Edge Cases Handled

  • Direct secret return: return %secret_arg still creates a generic (no ops but secret output)
  • Plaintext-only return: return constant does NOT create a generic
  • Mixed inputs: Non-secret inputs passed to secret ops work correctly

Example

Before (Issue #2553 - causing crash):

func.func @foo(%x: i32 {secret.secret}) -> i8 {
  %c = arith.constant 42 : i8
  return %c : i8
}
// Output: !secret.secret<i8> ← WRONG, crashes downstream

After (This PR):

func.func @foo(%x: !secret.secret<i32>) -> i8 {
  %c = arith.constant 42 : i8
  return %c : i8  // No generic, plaintext preserved
}

Testing

@pantha704 pantha704 force-pushed the fix/wrapgeneric-output-type branch 2 times, most recently from e574b35 to 7492928 Compare January 25, 2026 04:38
@pantha704 pantha704 force-pushed the fix/wrapgeneric-output-type branch from 7492928 to 03b7ba7 Compare January 25, 2026 04:45
}
}

// Track which operations are secret-dependent
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I think instead of cloning only secret ops in, I think this particular pattern should just be wrapping the entire block in a generic, but modifying just the selective output wrapping that you did with newOutputs.

There are other patterns that would also benefit from taking the solver in this file, but maybe we can go pattern by pattern to integrate the secretness analysis (for example, HoistPlaintextOps would benefit, and that would hoist plaintext ops outside of the secret body if they can be hoisted).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the core issue is that if you secret.yield a plaintext SSA value, it becomes secret according to the secretness analysis and won't be automatically converted to a public value. We had this special behavior back in the CGGI pipeline because we wanted to make an empty memref secret as the initializer for a loop that put secret items inside it.

Maybe the relevant pattern could have an option to control its behavior, and we could have this pass specialize... Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!op.getOps<memref::AllocOp>().empty()) {

Right - that's why I think this pattern should also take secretness analysis and we should test if that allocated value will be used for secret storing values later. (That being said, I think that would mean secretness analysis would need a backwards analysis as well). But I think that's why I'd prefer this pass stay minimal and tackle that problem in a later PR

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we agree, but that would make this issue unresolvable until the secretness analysis is improved.

Per maintainer feedback, this change:
- Keeps the original 'wrap entire block' approach
- Only changes output type selection based on SecretnessAnalysis
- Removes the complex op partitioning (cloning only secret ops)
- Fixes google#2553 by not creating generic when no outputs depend on secrets

The key insight from maintainers is that hoisting plaintext ops
should be handled by HoistPlaintextOps pass, not here.
@pantha704
Copy link
Author

Thanks for the feedback @asraa @j2kun!

I've simplified the implementation based on your guidance:

Changes

Key Logic

// Determine output types based on secretness analysis
for (auto [i, resultType] : llvm::enumerate(op.getResultTypes())) {
  Value returnVal = returnOp->getOperand(i);
  if (isSecret(returnVal, solver)) {
    newOutputs.push_back(secret::SecretType::get(resultType));
    hasSecretOutputs = true;
  } else {
    newOutputs.push_back(resultType);
  }
}

// Skip generic if no outputs depend on secrets
if (!hasSecretOutputs) {
  return success();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WrapGeneric forces output to secret type, leading to crashes later in the pipeline

3 participants