Godel had an anxiety disorder, and very probably was on the autistic spectrum. He thought everyone except his wife was trying to poison him. After his wife died, he pretty much stopped eating. He is of course considered a mathematician, but there can be no doubt his Incompleteness Theorems are among the most important philosophical insights of the last century.
Einstein would have been diagnosed as autistic as I understand it, because of his late language development - I don't think he spoke until he was 9. Again deep insights, also very much a humanist, and made powerful statements outside his field that are still widely quoted.
Martin Luther undoubtedly had a strong case of Obsessive Compulsive Disorder (details in the Sapolsky vid linked below), and his thoughts reshaped Christian philosophy and European politics.
Nietzsche is now thought to have been bipolar. He had a total nervous mental breakdown some ten years before he died, triggered by seeing a horse being badly beaten in the street. He burst into tears hugging the horse, and never wrote again. It used to be thought this was a result of tertiary syphilis, but scholars now think not.
So, I would approach this by looking at what 'mental illness' is. Nowadays, we might be more inclined to use the terms neurodivergent, and contrast to ordinary 'neurotypicals'. Biologist Robert Sapolsky describes here how mild schizophrenia and mild ADHD can be linked with prophecy and ritual activities, and how they can be related to evolutionary drivers. Certainly many philosophers, like Diogenes or Descartes were quite odd people, and could definitely be called neurodivergent.
But consider cognitive bias. We calibrate ill vs health, typical vs divergent, by looking at other people. But what happens when society is irrational? How do we truly work out when we are unbiased, rational, sane, without simply looking at what others do? Perhaps the 'ultimate' example of that is the world of the film Dr Strangelove, which our understanding from now declassified Coldwar nuclear policies show was terrifyingly accurate, and involved total annihilation of all human life for a range of trivial and likely to happen reasons. You can say there was an animal-rationality to it if it was only posturing, but it was not sane policy (see The Fog Of War documentary for how truly crazy it got).
Cultures & communities of practice seem to be the way. The sangha in Buddhism, aim to keep a collective practice of calming and observing the mind through meditation and other practices. The scientific community don't have a simple rule-based scientific method, but a culture of critique and scepticism, and practices like peer review. The Socratic Method is not so much a set of rules, but committing to thinking together, sceptically. We can generalise these, by looking at Wittgenstein’s framework of 'modes of life', with their own ways of using words, expectations about framing questions and what counts as answering them, and so on. This links to the Private Language argument, the most devastating critique not only of solipsism but also idealism, because our concepts and their refinements are understood to be inextricable from communities of practice, to be meaningless and impossible 'solo'.
Mental illnesses, and cognitive biases, may trigger or shape our inquiries. I have noted before that Hobbes's view of human nature (a war of all against all) vs Rousseau's (the noble savage), probably tell us more about how those individuals related to their own impulses, than about human nature. But through careful practice, and community comment, we can come to conclusions that go beyond those biases. Hume's point that "reason is and ought only be slave of the passions" parallels this, our deep motivations may always be opaque to us. The lesson of looking at cognitive biases is, as Robert Heinlein put "Man is not a rational animal, he is a rationalizing animal", we use our reasoning to navigate and move ourselves, not just to examine. We should look not only to our own clear thinking, but to communities that share it, to try and understand, define, and live, a more sane life.